Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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)

share|improve this question
add comment

5 Answers

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
share|improve this answer
add comment

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

share|improve this answer
add comment

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)

share|improve this answer
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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