Why does setting the bash prompt (PS1) manually cause root to have a $ instead of a #?

I'm on CentOS 6.4. By default the root user's prompt has # symbol, i.e. it looks something like [root@myserver ~]# .

echo $PS1 prints out [\u@\h \W]\$; but if I do PS1="[\u@\h \W]\$" (which shouldn't change anything, afaics), I end up with a prompt that looks like this: [root@myserver ~]$.

Why when I try to set it myself (I'd ultimately like to change it to something more useful), does it interpret the $ literally? Just about every page I can find (e.g., How to: Change / Setup bash custom prompt (PS1)) says that the \$ should result in a # for the root user.


Solution 1:

In your question, you imply that you're not quoting the line at all; that's clearly wrong, because then the space would separate the argument assignment "PS1=[\u@\h" from the command "\W]\$". Please realise that every character is important.

Anyway, what you're probably doing is using double quotes (") rather than single ones ('). They behave very different when it comes to the shell interpreting what you typed. With double quotes, it will try to do clever things with the sequences of backslash/some-character. For this assignment, since there's nothing special about \u, \h, and \W, they're left untouched; square brackets can have special meanings in various places, but not in this case, so they're also left in place; but since a $ has a variety of meanings, all of them special, the \$ sequence is interpreted as "ignore the specialness of the dollar sign, and just leave it there", and that sequence is replaced with a no-further-parsed single dollar sign. The result of all of that is assigned to PS1, and since there's a literal dollar sign, and not the magic \$ sequence when the shell constructs the actual prompt, you just get the dollar sign, and not the effective-user-dependent character. The actual parsing is a bit more complicated, but that's the gist of it for this particular example.

What you typed: PS1="[\u@\h \W]\$ "
What the shell assigned: PS1 <- "[\u@\h \W]$ "
What you wanted: PS1 <- "[\u@\h \W]\$ "
What you should type: PS1='[\u@\h \W]\$ '

What you should do next: Read something like this tutorial to learn more about shell quoting.