2

I'm using Ubuntu 16.04 LTS. I have a question for setting PS1 and using single quotation marks. I have been practicing to change a prompt. So, I have followed an example in the book, then I'm faced a curiosity.

The example is below:

user1@myubuntu:~$ PS1='$PWD '
/home/user1 

I learned that single quotation marks make special characters (like $, ``, \, etc.) to general characters.

So, I think the prompt above the statement must be like below:

user1@myubuntu:~$ PS1='$PWD '
$PWD

Why does the special character ($) still work in the single quotation marks?

  • type echo $PS1... – Zanna Sep 19 '17 at 12:33
3

Bash (and other shells) don't print PS1 as-is. They interpret it, and the display the output. Bash, for example, expands various special codes like \u, \w, etc., and then (from the Bash manual, Controlling the Prompt):

After the string is decoded, it is expanded via parameter expansion, command substitution, arithmetic expansion, and quote removal, subject to the value of the promptvars shell option (see Bash Builtins).

Parameter expansion (i.e., variable expansion) means that a string like $PWD in PS1 is expanded. If you want to show a plain $PWD in the prompt, do either of:

PS1='\$PWD '
# or
shopt -u promptvars
PS1='$PWD '

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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