Search code examples
bashsyntaxquotesps1

How can this PS1 variable be defined in single-quotes?


The default .bashrc file for Debian-based systems sets $PS1 like this:

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

What confuses me is that ${debian_chroot} part. According to StackOverflow: Difference between single and double quotes in bash, if single-quotes are used when defining a string, characters such as $ should be treated as a literal, rather than evaluate variables.

Doesn't this mean that Bash should actually print out ${debian_chroot:..., and not the value of that variable? Or are there more syntax rules involved here?


Solution

  • You'd normally be right, except the value of PS1 is expanded again at runtime as part of generating the prompt. This is specifically to allow expansions at runtime.

    PS1='$PWD: ' will expand $PWD when the prompt is shown, so that you always see the current directory.

    PS1="$PWD: " will expand $PWD when the prompt is defined, so that you always see the directory you were in when you defined the prompt.