sh: parse_git_branch: command not found
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.
- Change your .bash_profile so that it contains the full path to your home directory instead of just the tilde
- Copy all the related bash files to
/var/root
- Instead of running
sudo su
, dosudo 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 runningsh
instead ofbash
. (In some operating systems, those are the same thing, but in others it's not. I believe that MacOSX is one of those wheresh
andbash
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
![abhimanyuaryan](https://i.stack.imgur.com/H1Gyx.jpg?s=256&g=1)
abhimanyuaryan
I am just some random shitty coder trying to improve. Teach me daddy!
Updated on July 21, 2020Comments
-
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 over 8 yearsI am using Mac. I don't have .bashrc. I have bash_profile. Are both same?
-
Jenny D over 8 yearsIt's the same. I'll edit my answer to remove the confusion.
-
Jenny D over 8 yearsFixed. 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 over 8 yearsIf 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 over 8 yearswhat's PS1 you wrote above
-
Jenny D over 8 yearsIt's the format for the command line prompt. At the end of your file
.bash_prompt
, there's a line that starts withexport PS1
- that's where you've defined that your prompt should include the functionparse_git_branch
. -
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 likePS1
in.bashrc
, but do not export it. Since it is not exported, it will not be in theroot
shell's environment, solving the problem. -
chepner over 8 yearsAlso,
/bin/sh
in Mac OS X isbash
. -
Jenny D over 8 years@chepner But it doesn't read the
.bashrc
or.bash_profile
files, right? -
chepner over 8 yearsCorrect; since the root shell is almost certainly
/bin/sh
, it won't execute anything startup files;sudo -i
andsu -l
would specify to simulate a login, meaning/etc/profile
and.profile
would be executed.