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
  • 207,228
H2ONaCl
  • 10,039

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
  • 207,228
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.