0

From the default .bashrc is this:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac 

The result is examined by issuing the echo command:

$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$

It appears that the ${debian_chroot:+($debian_chroot)} conditional insertion will be performed for the command line prompt but not for the terminal title. It appears the .bashrc author intended it to be displayed in both places just to the left of user@host but in fact it did not make it into both places. The evaluation of debian_chroot happened in defining PS1 but it was not meant to be evaluated at that time, rather it was meant to be evaluated at the time of prompting. The root of the problem is that when building strings "" quoting evaluates and '' quoting does not. The evaluation was early because the whole thing enclosed in \[ \] was meant for the title. Is interpretation correct?

muru
  • 197,895
  • 55
  • 485
  • 740
H2ONaCl
  • 9,693

2 Answers2

3

I would say the whole thing is irrelevant. debian_chroot is set in /etc/bash.bashrc. It is a fair bet that, in the normal course of things, if the variable was set, a new shell has been started - why would /etc/bash.bashrc be sourced otherwise? And in that case, ~/.bashrc would be sourced as well. So, either the variable is set when PS1 is set and modified in the lines you show, or it isn't, and it won't be. The effect is the same.

I can only guess at why the original developer used single quotes while setting PS1 in the first place. Presumably, the dev has learned caution and uses "" only when necessary, which it is when modifying PS1.

Note that :+ just means do nothing if the variable is unset.

muru
  • 197,895
  • 55
  • 485
  • 740
  • How does PS1 manage to put $debian_chroot into the terminal title? – H2ONaCl Dec 30 '15 at 01:02
  • @H2ONaCl if debian_chroot was set, its value would be in the title-setting part. – muru Dec 30 '15 at 01:04
  • But then would you not expect PS1 to contain two instances of ${var:+var}. Once for the prompt and once for the title. My PS1 has only one instance. – H2ONaCl Dec 30 '15 at 01:11
  • @H2ONaCl have you read my answer at all? Either the variable is set when PS1 is being set, or it isn't. The instance that you speak of meaningless, since the variable is unset. If it were set, you'd have the content in the title-setting part, and the instance you see now would still be there, and this time it would evaluate to something. Do us all favour and test it: debian_chroot=foo bash. – muru Dec 30 '15 at 01:14
  • So you think ${var:+var} should be available for evaluation for the purpose of the title when .bashrc is invoked, but not available later for the evaluation of PS1. And for the prompt ${var:+var} should not be evaluated when .bashrc is invoked, but available to be evaluated in PS1 later? I don't understand the inconsistency. – H2ONaCl Dec 30 '15 at 01:21
  • @H2ONaCl What? When did I say anything like that? – muru Dec 30 '15 at 01:22
  • I mean do you think the .bashrc is okay with respect to setting PS1 such that the title part at the time of displaying the title will not evaluate ${var:+var} given the var was undefined for me when .bashrc was invoked but the prompt part can evaluate ${var:+var} at the time of displaying the prompt because it was included in PS1 as a literal. – H2ONaCl Dec 30 '15 at 01:27
  • @H2ONaCl Again, have you read my answer at all? Go back, read it again, and tell me when debian_chroot is set. – muru Dec 30 '15 at 01:28
1

${debian_chroot:+($debian_chroot)} insertion depends on the presence of $debian_chroot variable or /etc/debian_chroot file.

Here's a simple example. In the screenshot bellow I'm connected through ssh to my Raspberry Pi with Ubuntu Snappy on it, which has $debian_chroot variable set ( by default ), while my laptop's Ubuntu does not. You can see the resulting prompt in the tabs, and in both cases variable for $TERM is xterm

enter image description here

Now if the variable $TERM was something else, it wouldn't alter the GUI terminal title. That could be connecting to a screen session, for example, where variable $TERM is screen.

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497