0

I'm a linux and Bash newbie and would like to ask help understanding the role of single and double quotes when assigning the PS1 env var. I wanted to set the terminal to always show the current directory. At first I tried this:

export PS1="\[\e[38;5;46m\]>\[\e[38;5;255m\]\[\e]0;$(pwd)\007\]"

This caused the terminal title updated to show the current directory when I hit Enter but not if I subsequently cd'ed to somewhere else.

Through aimless trial-and-error, I eventually tried:

export PS1='\[\e[38;5;46m\]>\[\e[38;5;255m\]\[\e]0;$(pwd)\007\]'

The second version, using the single-quotes, had my desired effect: the terminal title updated every time I cd'ed to a new directory.

Can someone explain why the use of single and double quotes differs this way? Thank you.

I found this discussion: How to change the title of the mintty window? but the OP seems to have not flagged an answer, and those answers that I tried from that link seemed to have no effect in my shell.

0

The difference between single quotes and double quotes is interpolation.

In the first example, the shell interpolates the expression between the double quotes before assigning it to the PS1 variable, so the value of the variable, after interpolation, would be

> /home/user

In the second example, because of the single quotes the shell literally assigns the expression to the variable, which causes the expected behaviour (every time a prompt is printed, bash interpolates the expression based on the current directory). The value of PS1 would be:

\[\e[38;5;46m\]>\[\e[38;5;255m\]\[\e]0;$(pwd)\007\]

You can easily check this with a couple of commands:

$ a='hello'
$ b="${a} world"
$ c='${a} world'

$ echo $b
hello world
$ echo $c
${a} world

In $b, the variable a is interpolated into the string, but in $c it is not, so the string is literally printed.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.