user3162553 user3162553 - 1 year ago 68
Linux Question

Bash PS1 Doesn't Evaluate Variables

I'm trying to set my PS1 prompt to

/usr/local/src mario

So I've used the following code inside of .bash_profile:

export PS1=" \w \u\[$(tput sgr0)\]"

And I end up with:

\w \u\[$(tput sgr0)\]


I can use string concatenation to get what I want but that seems very odd that I need to do that.

Answer Source

Your shell is likely not bash. Change it with chsh.

Also, there's a problem with part of the $PS1 value:

PS1=" \w \u\[$(tput sgr0)\]"

The $(...) will be evaluated when you set PS1, but just that one time. If you would like to execute tput sgr0 every time the prompt displays, use single quotes:

PS1='\w \u\[$(tput sgr0)\] \$ '

(I have also moved the initial space from the start to the end of the value and added \$ which will make the prompt and command line more readable.

You could also set

PS1="\w \u \$ "

and then set

PROMPT_COMMAND="tput sgr0"

to have tput sgr0 execute before displaying every prompt (I don't know if it makes sense to do this as I don't quite know what it does, but the facility for it is there).

Notice that neither PS1 nor PROMPT_COMMAND needs to be exported as it only makes sense to set them for the current shell session.