13

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.

2
  • PS1=[\u@\h \W]\$ returns -bash: W]$: command not found.
    – choroba
    Dec 9, 2013 at 13:22
  • @choroba oops, sorry, missed the double quotes off, it should have been PS1="[\u@\h \W]\$". Dec 9, 2013 at 13:50

2 Answers 2

18

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.

2
  • 1
    Yep, you were spot on, I was using double quotes (sorry for missing them from the original question), thanks for pointing out my mistake. Dec 9, 2013 at 13:52
  • If you ever do need to use double-quotes for some reason, use PS1="...\\\$" to ensure that the literal pair \$ is stored in PS1 for the shell to interpret later.
    – chepner
    Dec 9, 2013 at 17:07
0

Have you switched to the root user using sudo su, su or by logging in as root? Use id to check who the computer considers you to be logged in as, as it may affect which profile you are actually editing.

Check out the answers to https://askubuntu.com/questions/305052/changing-behavior-of-bash-prompt-when-functioning-as-root

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .