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 have a function defined in my user's .bashrc file that displays my current git branch name at my command prompt:

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1="\\[$(tput setaf 7)\\]\\w \$(parse_git_branch)> \\[$(tput sgr0)\\]"

When I sudo to su, it looks like my PS1 is carried over, and after every command I get the error bash: parse_git_branch: command not found.

What can I change so that when I sudo to another user, its PS1 is used and not that of my user?

Edit: It seems like this only happens after I use source a Python virtualenv. If I sudo su before I source the virtualenv, this error does not occur.

share|improve this question
    
maybe moving this functuon form bashrc to .profile could solve the problem - try it out. –  mnmnc Mar 21 '13 at 15:30
    
@mnmnc .profile is only read by login shells. –  terdon Mar 21 '13 at 15:41
    
What error are you getting? It seems to cause no problems on my system. sudo su has the normal PS1. –  terdon Mar 21 '13 at 15:42
    
@terdon I'm not getting any errors other than "command not found" when I sudo su. –  skyler Mar 21 '13 at 15:44
    
Actually it seems like this only happens after I use source a Python virtualenv. If I sudo su before I source the virtualenv, this error does not occur. –  skyler Mar 21 '13 at 15:46

2 Answers 2

up vote 1 down vote accepted

Did you ever expor PS1 somewhere (e.g. in your Python virtualenv)? I always have this line in my .bashrc or .bash_profile:

export -n PS1 # unexport PS1 so sub-processes will not inherit it

So try export -n PS1 before you sudo.

share|improve this answer

PS1 is not being reset by sudo, it's kept by default. Functions are part of the bash environment, but are not preserved by sudo. You can do any of several things:

  • instead use sudo su - so you get a login shell, that will reset PS1
  • set env_reset to "clean up" the environment, possibly with env_keep
  • set SUDO_PS1 and sudo will place that value in PS1
  • use sudo -i which is similar to su - (you'll need to add your shell to the sudoers file)
  • check the variable SUDO_COMMAND in your .bashrc and reset PS1

The list of variables that sudo keeps (may vary by version, check env.c in the source) includes:

DISPLAY COLORS LS_COLORS HOSTNAME PS1 PS2 TZ

This is a hardcoded list, it's not the same thing as env_keep (i.e. "env_keep -=" won't change it).

Run sudo -V as root for a full list of variables preserved or removed (for sudo version >= v1.7).

One final suggestion, you could make your PS1 more robust by checking if the function exists:

PS1="\\[$(tput setaf 7)\\]\\w \$(type -t parse_git_branch >/dev/null && parse_git_branch)> \\[$(tput sgr0)\\]"
share|improve this answer
1  
Usually an env var would not be passed to the child processes unless the var is exported. So if PS1 is not exported sudo will not see or keep it. –  bzjywhjm Mar 24 '13 at 10:32
    
Yes you're perfectly correct, usually it's wanted (so you don't need to fix it in subshells), but you can also export -n PS1 so that it's not exported. –  mr.spuratic Mar 24 '13 at 20:29

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.