Bash: Switching from user to root causes issues with prompt

0

I am on a Mac, running Snow Leopard (if it matters). I am customizing my bash prompt to display things that get pulled from user specific places. For example, I am using Ruby Version Manager to manage different versions of Ruby. I am displaying the Ruby version I am using in the prompt. I am also using Homebrew, and have installed Git via Homebrew. I am displaying the Git branch in the prompt as well.

The problem is when I switch to root (sudo su, or sudo bash) it tries to use my user's prompt, which it won't be able to use because it won't be able to find RVM or Homebrew. So it displays errors. How can I fix this so that it doesn't display errors? (either by bringing RVM/Homebrew over as if I were still that user, or not displaying my custom prompt)

Andrew

Posted 2011-10-12T15:49:23.047

Reputation: 11 982

Answers

1

Option 1

Use sudo su - instead of just sudo su

The extra dash is a synonym for -l which simulates a full login. This means it will read config files (.bashrc, .profile, etc) from the root user's home directory.

Option 2

If you insist on using your own .bashrc, put a test in to check for the existence of RVM in the path before trying to put it in the path. Something like this:

type rvm > /dev/null
RVM_EXISTS=$?  # get exit status, 0 if it exists
if [ $RVM_EXISTS ] 
then
    PS1=(your rvm code here)
else
    PS1=(your prompt w/o rvm here)
fi

(not tested)

Option 3

Test for existence of rvm as in option 2, but create a dummy rvm bash function if it doesn't exist and don't alter PS1

type rvm > /dev/null
RVM_EXISTS=$?  # get exit status, 0 if it exists
if [ ! $RVM_EXISTS ] 
then
   rvm () { echo > /dev/null }
fi

Doug Harris

Posted 2011-10-12T15:49:23.047

Reputation: 23 578

0

You need to change your prompt by manipulating the system variable $PS1

If you wanted to simply display your hostname followed by a dollar sign, you could do the following: export PS1='h:\W\$'

To keep your settings for the next Bash shell, place a line in your $HOME/.bashrc that looks like this: PS1='\h:\W\$'

Mac users note the following Due to the way OS X and the Terminal application handle command emulation you have to create the file $HOME/.bash_profile and enter the following line: source ~/.bashrc

Source

dekyos

Posted 2011-10-12T15:49:23.047

Reputation: 301

0

You can set it all up in the root's home directory so that it uses a different .bashrc, or the same one (just mv it)

Rob

Posted 2011-10-12T15:49:23.047

Reputation: 2 152

0

I'm not entirely sure, but I believe that running sudo -s will start root shell with all your environment variables still in effect.

chronos

Posted 2011-10-12T15:49:23.047

Reputation: 1 149

0

Won't let me vote, but Doug Harris has the correct answer: "Use sudo su - instead of just sudo su"

That is by far the easiest way to do it. And it works when going sudo on any user to assume their login environment. I personally would not mess around with root's login environment and would use "sudo su -" instead.

JJRIV

Posted 2011-10-12T15:49:23.047

Reputation: 1