Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.) says that the $ should result in a # for the root user.

share|improve this question
    
PS1=[\u@\h \W]\$ returns -bash: W]$: command not found. –  choroba Dec 9 '13 at 13:22
    
@choroba oops, sorry, missed the double quotes off, it should have been PS1="[\u@\h \W]\$". –  stripybadger Dec 9 '13 at 13:50
add comment

2 Answers

up vote 7 down vote accepted

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.

share|improve this answer
    
Yep, you were spot on, I was using double quotes (sorry for missing them from the original question), thanks for pointing out my mistake. –  stripybadger Dec 9 '13 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 '13 at 17:07
add comment

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 http://askubuntu.com/questions/305052/changing-behavior-of-bash-prompt-when-functioning-as-root

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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