Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sh: parse_git_branch: command not found

Tags:

git

bash

macos

su

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\]"
like image 515
abhimanyuaryan Avatar asked Nov 23 '15 06:11

abhimanyuaryan


2 Answers

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.)
like image 187
Jenny D Avatar answered Oct 11 '22 15:10

Jenny D


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

like image 25
Walterwhites Avatar answered Oct 11 '22 15:10

Walterwhites