14

Before you click away, this isn't the typical "how do I make my bash prompt have color" question. I've already customized my bash prompt to look like this:

[user @ host]----[$(pwd)]
$ 

where everything in brackets is light blue, and everything else (including $) is black by adding the following to my ~/.bashrc file

# Turn the prompt symbol red if the user is root
if [ $(id -u) -eq 0 ];
then # you are root, make the prompt red
    PS1="[\e[01;34m\u @ \h\e[00m]----[\e[01;34m$(pwd)\e[00m]\n\e[01;31m#\e[00m "
else
    PS1="[\e[01;34m\u @ \h\e[00m]----[\e[01;34m$(pwd)\e[00m]\n$ "
fi

The goal is to make it such that the only thing that changes when I use 'sudo su' is that the black $ changes into a red #. I've looked in /etc/bash.bashrc and /etc/profile to see if there is just a line to comment out, but there is a bunch of stuff about debian_chroot that I don't understand, and I don't want to screw something up. How can I accomplish what I want?

P.S. This is what I want the prompt to look like as root

[user @ host]----[$(pwd)]
(red)#

edit: Mark this solved, appending the above code to ~/.bashrc while root accomplished my goal. Also, in the above code, $(pwd) only displays the home directory (I guess because that is the working directory when the terminal is opened), and never updates. Replacing $(pwd) with \w fixes this, but displays the home directory as ~, which I was trying to avoid.

  • Are you using $pwd or $PWD? – belacqua Jun 7 '13 at 0:30
  • It'll fail to wrap lines correctly since you did not tell bash that you were using non-printable characters. Enclose non-printable characters, such as \e[01;31m, in \[...\]. This is explained under PROMPTING in the manual. Also see mywiki.wooledge.org/BashFAQ/053 – geirha Jun 7 '13 at 7:39
  • Reason why pwd did not work in the prompt is, that you need to escape the dollar-sign, otherwise $(pwd) will be executed before the content is placed in $PS1. You can test it with this: PS1="\$(pwd) ". – F-3000 Dec 10 '15 at 9:30
11

It will also depend on how you become the root user. You need to make the change in the root user's .bashrc if you are using something like su - root or sudo -i, where you read in the environment.

With sudo -s, you should be reading your own .bashrc.

Consider adding some printf or echo statements to debug your code, to tell you when it has executed.

Use the id command to make sure you are who you think you are:

root@tau:~# id
uid=0(root) gid=0(root) groups=0(root)
  • I'm using sudo su – zmitchell Jun 6 '13 at 23:14
  • 1
    sudo su reads ~root/.bashrc (at least as currently implemented on my 13.04 system). – belacqua Jun 6 '13 at 23:22
3

To change the colour of the prompt to red for root, I modified the .bashrc file, following the examples in this thread.

regularuser@myubuntubox:~$ sudo su - 
root@myubuntubox:~# vim .bashrc

Then, in vim, search for the final appearance of PS1 assignment, and add this following it (note that it updates the value of $PS1 so that other previous lines do not need to be modified, and the change is easily revertible):

# Set RED prompt
PS1="\[\e[01;31m\]$PS1\[\e[00m\]"
2

Following belacqua's valid answer https://askubuntu.com/a/305053/12218, by issuing sudo su the root's .bashrc is loaded.

You can check this by issuing sudo su followed by cd ~ you will see that you are at root's home directory.

Using sudo su you log in as root (su = switch user) in contrary with
sudo do some administrative task as user with elevated privileges.

So by using sudo su you become the actual root and the changes made to environment are changes made specifically to root, i.e: sudo su followed by vim .bashrc.

0

Following may help

export PS1='`[ $(id -u) == "0" ] && echo "#" || echo ">"` '

This will show '> ' for normal user and '# ' for root. This still needs to be in root's .bashrc too.

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.