2
  1. .bashrc and bash.bashrc include:

    [ -z "${debian_chroot:-}" ]
    
  2. Bash manpage states the following on parameter expansions of the following form: ${parameter:-word}

    If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

  3. Why write

    "${debian_chroot:-}"
    

    and not simply

    "${debian_chroot}"
    

    ?

Answer I'm going with: https://www.reddit.com/r/Ubuntu/comments/3xb9sp/debian_chroot_why_bother_with/

  • I've updated my post once more. The post you claim is duplicate doesn't answer the question I am asking. Here is an answer: https://www.reddit.com/r/Ubuntu/comments/3xb9sp/debian_chroot_why_bother_with/ – stöpsel_neaty Dec 18 '15 at 13:29
  • If you find that linked answer acceptable, would you mind, copying it into an answer? – David Foerster Dec 18 '15 at 13:54
  • If you find that linked answer acceptable, would you mind, copying it into an answer? – David Foerster Dec 18 '15 at 13:56
  • @DavidFoerster Can I give an answer to my own question? I put the link in the original post. – stöpsel_neaty Dec 18 '15 at 15:01
  • Yes, you can give an answer to my own question and also click the check mark beside your answer to mark it as accepted two days after when you originally posted the question. – karel Dec 18 '15 at 16:02
  • I agree with Karel. See [Can I answer my own question?](/help/self-answer) – David Foerster Dec 18 '15 at 19:55
  • I have a reputation of 6 and this possibility is available from 15 points. If someone else wants to answer my question with the link I posted, I'd be happy to mark it as answered. – stöpsel_neaty Dec 20 '15 at 08:27

1 Answers1

1

The part that you mention comes earlier in the .bashrc, while what's mentioned in the duplicate comes a little bit later, in the PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' part. They work together, though !

So what does the code you mention say in terms of plain English ? Here it is for reference.

if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

It starts with this part ${debian_chroot:-}. Do we have that debian_chroot variable set ? If yes, echo that variable, but if not - echo whatever comes after -. And what comes after - ? Nothing ! Exactly that ! Why bother ? What if the variable is set ? Then we don't need to execute body of the if statement and just use debian_chroot variable as is inside PS1 prompt later. Remember I said those two pieces of code work together ? OK, moving on.

Next step, if we evaluated the variable to be blank, we check for existance of readable /etc/debian_chroot file. On Ubuntu 14.04 at least , there's no /etc/debian_chroot file. So whenever you start bash it goes "Oh, so debian_chroot is not set and we don't have that file there . . .OK, leave only \u@\h:\w\$ inside the prompt ! "

Now what happens if we DO have that file ?

xieerqi:
$ sudo vi /etc/debian_chroot
[sudo] password for xieerqi: 

xieerqi:
$ bash
(HELLOWORLD)xieerqi@eagle:~$ echo $debian_chroot
HELLOWORLD
(HELLOWORLD)xieerqi@eagle:~$ 

OK, trace the code again : do we have debian_chroot set ? No. Do we have that /etc/debian_chroot file ? Yes, so take its contents , and shove them into this users PS1 prompt. Looks like that's exactly what happened in my output, right ?

Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
  • Yes, why bother substituting an empty string to a parameter only when that parameter is known to be empty? Here is an answer: https://www.reddit.com/r/Ubuntu/comments/3xb9sp/debian_chroot_why_bother_with/ – stöpsel_neaty Dec 18 '15 at 13:28