1

I'm willing to change my command prompt to be something like

user$

where user is not root and

root#

When the current user is root. I've searched for that and found that this is achieved through the \$ special character and now I have something like this in my ~/.bashrc

PS1="\u\$ "

Unfortunately, the \$ is not displayed as a # when the current user is root. For example, after executing

user$ sudo bash

I get my command prompt

root$

Instead of

root#

Am I missing something?

  • Yes, you set it in your .bashrc, not root's. – muru Feb 16 '15 at 18:55
  • @muru added the same assignment to /root/.bashrc, but nothing appears to be changed! – Kareem Feb 16 '15 at 18:58
  • Presumably because some variable still points to you as the use. Try sudo -i. – muru Feb 16 '15 at 19:02
2

Add the following line in your ~/.bashrc

if [ "$(whoami)" = "root" ]; then PS1="\u# "; else PS1="\u\$ "; fi

when you open a new terminal you will get,

user$
user$ sudo bash
root#

Note: It will work only with sudo bash. As sudo -i or sudo su will check /root/.bashrc instead of user's .bashrc. To get the same behavior copy the same in /root/.bashrc also.

Alternate Better Way:

The same thing can be achieved by setting PS1 as

PS1="\u\\$ "          

in user's as well as root's .bashrc

  • It is. Thank you so much! Is the question confusing? Do you suggest any edits? – Kareem Feb 16 '15 at 19:53
  • @Kareem no I just overlooked that line, sorry. – souravc Feb 17 '15 at 3:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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