LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash prompt doesn't change from $ to # for su (https://www.linuxquestions.org/questions/linux-newbie-8/bash-prompt-doesnt-change-from-%24-to-for-su-858485/)

mike11 01-25-2011 03:24 AM

bash prompt doesn't change from $ to # for su
 
Hi All,

I'm trying to change the bash prompt and based on the man pages \$ should show a $ which changes to a # for a SU. However, this doesn't happen on my machine, it's $ for both user and SU.
The line in .bashrc is:
export PS1="\u@\h:\w\$"

Thanks in advance.

crts 01-25-2011 03:26 AM

Hi,

I assume you mean the prompt for root. You will have to edit root's .bashrc for that. It is located in /root/.bashrc.
Also: Make sure that you use
su -
to switch to root. This will give you a full login shell where root's .bashrc is definitely being sourced.

mike11 01-25-2011 03:40 AM

Thanks. The root's .bashrc is symlinked to mine. So if I have \$ on my prompt shouldn't this work when changing to su?.

grail 01-25-2011 03:43 AM

Well if it is symlinked to the same thing ... why would it change?? Remembering '\$' means take away the special meanings for the $ sign to give you a $ sign.

Kenhelm 01-25-2011 03:58 AM

Try using single quotes instead of double quotes
export PS1='\u@\h:\w\$'

Single quotes preserve the '\' in front of '$'
echo "\u@\h:\w\$"
\u@\h:\w$

echo '\u@\h:\w\$'
\u@\h:\w\$

mike11 01-25-2011 03:59 AM

well, the man page says:
Quote:

\$ if the effective UID is 0, a #, otherwise a $
so I su and do echo $EUID and I get 0
which means, as I understand, that the $ should turn into a #. why does the symlink change anything? it's just a pointer to a .bashrc file to run.

note the PS1 line is in double quotes (so it shouldn't be a literal $):
export PS1="\u@\h:\w\$"

crts 01-25-2011 04:19 AM

Quote:

Originally Posted by mike11 (Post 4236864)
well, the man page says:

so I su and do echo $EUID and I get 0
which means, as I understand, that the $ should turn into a #. why does the symlink change anything? it's just a pointer to a .bashrc file to run.

note the PS1 line is in double quotes (so it shouldn't be a literal $):
export PS1="\u@\h:\w\$"

I still think it is better to not link root's bashrc to a user ones. However, if you want to keep it that way then you have to replace the double quotes with single quotes - as suggested be kenhelm. Then you will get the '#' when you switch to root. Make sure to check if there are several branches which set PS1 in your script. If you are not sure which one sets the prompt then make sure to change all of them to single quotes.

mike11 01-25-2011 04:23 AM

It's working with single quotes. Thanks.


All times are GMT -5. The time now is 12:04 PM.