Mac OS X, how do I set the bash shell prompt (PS1) that every time the user type sudo su (root mode) the shell prompt change from the standard color to the red color and of course if the user exit from the root mode the color revert back to the default color. Basically what I am trying to do, is to warning the user that he/she is in root mode, so be careful.

I know I can put the following line PS1="\e[0;31m[\u@\h \W]\$ \e[m " inside the user personal .bash_profile but I guess this is not the correct approach.

  • 1
    What have you tried so far? – nohillside Jun 27 '19 at 17:35
  • So far I only vim .bash_profile and I added the following line: PS1="\e[0;31m[\u@\h \W]\$ \e[m " but once again it does not help. I want to be red only if the user is within the root. Example if I type sudo su after I authenticate my password, I want the following sh-3.2# to be red color – Fabio Viola Jun 27 '19 at 19:44
  • Much more important is that you stop doing things like 'sudo su', especially since you think it's something called "root mode", which it is not. – Marc Wilson Jun 27 '19 at 23:40
  • Thanks Marc. So what is the exact mode I should call when I am in this mode: sh-3.2# – Fabio Viola Jun 28 '19 at 13:40

One approach is to define your prompt in your .bashrc-

#sudo -s in .bashrc
case $LOGNAME in
root) PS1="\e[0;31m[\u@\h \W]\$ \e[m "
   *) PS1="\h:\W \u\$ "

and place the following in your .bash_profile-

case $- in
        *i*) . ~/.bashrc ;;

This will source .bashrc if your shell is a login interactive shell, otherwise .bashrc is read if your shell is a non-login interactive shell. Users are encouraged to use sudo -s instead of sudo su.

The advantage is that the invoking user keeps their environment and shell with sudo -s.

| improve this answer | |
  • fd0 my apologies. I guess i did not express myself correctly. Basically every time the user type (sudo su) on their mac os x system I want the prompt to be changed to red color. – Fabio Viola Jun 28 '19 at 13:55
  • @Fabio Viola- Well, at first I missed the su in sudo but was offering another approach. sudo su will produce a non-login interactive shell. The manual states that sh reads /etc/profile and ~/.profile when invoked as a login shell then ENV if set and a non-login interactive shell would read the file assigned to the ENV variable. ENV and ~/.profile can also be read by zsh and ksh so you need to shield these files from bashism. – fd0 Jun 28 '19 at 14:20
  • @fd0 And who's bash_profile and bashrc will it be reading? – Marc Wilson Jun 28 '19 at 23:46

You must log in to answer this question.

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