Search code examples
bashterminalps1

How the PS1 of the title doesn't overwrite the PS1 of the prompt


I noticed that in ~/.bashrc there are two sets of PS1:

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 first one checks for $color_prompt, if it true or not and decides for the text of the prompt.
The second one checks for $TERM and decides for the title.

But both of them changes the same environment variable, so when the terminal starts and "see" that PS1 is set for the text of the prompt, how it can also set the title? Is ~/.bashrc being loaded twice?


Solution

  • The first condition sets $PS1 according to whether $color_prompt is enabled or not. The second assigned extends this by setting $PS1 to an expression the contains the previous value of $PS1

    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h:\w\a\]$PS1"
    # Here ---------------------------------------------------^