sh: parse_git_branch: command not found

23,556

Solution 1

The problem here is that when you do sudo su, you are changing to root but you are keeping your own profile. That profile contains a setting for the command prompt that references a bash function. But when you sudo to root, you are getting root's shell, which is sh instead of bash - so any modifications that rely on bash configurations will not work, including the function you are referencing in your PS1.

So, the first thing to do is to make sure that you are actually running bash instead of sh when you sudo. This is very simple - instead of running sudo su, you simply run sudo bash.

Since sudo defaults to switching to root, you will now be running the bash shell as root, instead of just switching to the root user's default shell.

If you still have problems, this may be due to your .bash_profile containing a reference to the current user's home directory, in that it points to ~ in these lines:

for file in ~/.{path,bash_prompt,exports,aliases,functions,extra}; do
  [ -r "$file" ] && source "$file"
done

When you run bash as yourself, ~ will expand to your own home directory - but when you run it as root, it will evaluate to /var/root and that's where it will look for your files.

There are three ways you can fix this; choose whichever one you prefer.

  1. Change your .bash_profile so that it contains the full path to your home directory instead of just the tilde
  2. Copy all the related bash files to /var/root
  3. Instead of running sudo su, do sudo su -. This will give you root's environment instead of your own. The downside is that all your own environment modifications will not be available to you, and you will be running sh instead of bash. (In some operating systems, those are the same thing, but in others it's not. I believe that MacOSX is one of those where sh and bash are different things.)

Solution 2

have you export your $PS1 ? You can check by run command:

printenv

else you should export it by run:

export -n PS1

after you will can run sudo or sudo su without problem

Share:
23,556
abhimanyuaryan
Author by

abhimanyuaryan

I am just some random shitty coder trying to improve. Teach me daddy!

Updated on July 21, 2020

Comments

  • abhimanyuaryan
    abhimanyuaryan almost 4 years

    I have root enabled on osx El Captain. I tried some of the solution already provided on stackoverflow and supersu but couldn't fix the error. I exported function parse_git_branch() to .bash_profile from .bash_prompt but I still get this error. I don't know bash scripting so I have no idea what's going on and what is to be fixed.

    abhimanyuaryan at Macbook in ~
    $ sudo su
    sh: parse_git_branch: command not found
    root at Macbook in /Users/abhimanyuaryan
    

    .bash_profile

    if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
    
    # Add Homebrew `/usr/local/bin` and User `~/bin` to the `$PATH`
    PATH=/usr/local/bin:$PATH
    PATH=$HOME/bin:$PATH
    export PATH
    
    # Load the shell dotfiles, and then some:
    # * ~/.path can be used to extend `$PATH`.
    # * ~/.extra can be used for other settings you don’t want to commit.
    for file in ~/.{path,bash_prompt,exports,aliases,functions,extra}; do
      [ -r "$file" ] && source "$file"
    done
    unset file
    

    .bash_prompt

    # @gf3’s Sexy Bash Prompt, inspired by “Extravagant Zsh Prompt”
    # Shamelessly copied from https://github.com/gf3/dotfiles
    # Screenshot: http://i.imgur.com/s0Blh.png
    
    if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then
      export TERM=gnome-256color
    elif infocmp xterm-256color >/dev/null 2>&1; then
      export TERM=xterm-256color
    fi
    
    if tput setaf 1 &> /dev/null; then
      tput sgr0
      if [[ $(tput colors) -ge 256 ]] 2>/dev/null; then
        # Changed these colors to fit Solarized theme
        MAGENTA=$(tput setaf 125)
        ORANGE=$(tput setaf 166)
        GREEN=$(tput setaf 64)
        PURPLE=$(tput setaf 61)
        WHITE=$(tput setaf 244)
      else
        MAGENTA=$(tput setaf 5)
        ORANGE=$(tput setaf 4)
        GREEN=$(tput setaf 2)
        PURPLE=$(tput setaf 1)
        WHITE=$(tput setaf 7)
      fi
      BOLD=$(tput bold)
      RESET=$(tput sgr0)
    else
      MAGENTA="\033[1;31m"
      ORANGE="\033[1;33m"
      GREEN="\033[1;32m"
      PURPLE="\033[1;35m"
      WHITE="\033[1;37m"
      BOLD=""
      RESET="\033[m"
    fi
    
    export MAGENTA
    export ORANGE
    export GREEN
    export PURPLE
    export WHITE
    export BOLD
    export RESET
    
    function parse_git_dirty() {
      [[ $(git status 2> /dev/null | tail -n1) != *"working directory clean"* ]] && echo "*"
    }
    
    function parse_git_branch() {
      git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1$(parse_git_dirty)/"
    }
    
    export PS1="\[${BOLD}${MAGENTA}\]\u \[$WHITE\]at \[$ORANGE\]\h \[$WHITE\]in \[$GREEN\]\w\[$WHITE\]\$([[ -n \$(git branch 2> /dev/null) ]] && echo \" on \")\[$PURPLE\]\$(parse_git_branch)\[$WHITE\]\n\$ \[$RESET\]"
    export PS2="\[$ORANGE\]→ \[$RESET\]"
    
  • abhimanyuaryan
    abhimanyuaryan over 8 years
    I am using Mac. I don't have .bashrc. I have bash_profile. Are both same?
  • Jenny D
    Jenny D over 8 years
    It's the same. I'll edit my answer to remove the confusion.
  • Jenny D
    Jenny D over 8 years
    Fixed. I have been working with Linux, MacOSX and FreeBSD during the past week, I sometimes get mixed up over which filename goes where...
  • Jenny D
    Jenny D over 8 years
    If anyone looks at the edit history of this answer, let me just say that I completely missed the actual problem at my first go at this question...
  • abhimanyuaryan
    abhimanyuaryan over 8 years
    what's PS1 you wrote above
  • Jenny D
    Jenny D over 8 years
    It's the format for the command line prompt. At the end of your file .bash_prompt, there's a line that starts with export PS1 - that's where you've defined that your prompt should include the function parse_git_branch.
  • chepner
    chepner over 8 years
    .bashrc and .bash_profile are not the same, but on Mac OS X a terminal window will usually open a login shell (and thus use .bash_profile) instead of a regular interactive shell (which would use .bashrc). Standard practice is to put things like PS1 in .bashrc, but do not export it. Since it is not exported, it will not be in the root shell's environment, solving the problem.
  • chepner
    chepner over 8 years
    Also, /bin/sh in Mac OS X is bash.
  • Jenny D
    Jenny D over 8 years
    @chepner But it doesn't read the .bashrc or .bash_profile files, right?
  • chepner
    chepner over 8 years
    Correct; since the root shell is almost certainly /bin/sh, it won't execute anything startup files; sudo -i and su -l would specify to simulate a login, meaning /etc/profile and .profile would be executed.