IQAndreas
IQAndreas

Reputation: 8468

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?

Upvotes: 3

Views: 730

Answers (2)

chepner
chepner

Reputation: 531165

It's often simpler to use PROMPT_COMMAND to set the value of PS1 each time it is used. This way, you don't have to escape code in PS1 itself.

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

PROMPT_COMMAND='make_prompt'

Upvotes: 0

that other guy
that other guy

Reputation: 123470

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.

Upvotes: 5

Related Questions