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/

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
  • 105,154
  • 20
  • 279
  • 497
  • 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