sh: parse_git_branch: 找不到命令

sh: parse_git_branch: command not found

我在 osx El Captain 上启用了 root。我尝试了 Whosebug and supersu 上已经提供的一些解决方案,但无法修复错误。我将 function parse_git_branch().bash_prompt 导出到 .bash_profile,但我仍然收到此错误。我不知道 bash 脚本,所以我不知道发生了什么以及要修复什么。

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="3[1;31m"
  ORANGE="3[1;33m"
  GREEN="3[1;32m"
  PURPLE="3[1;35m"
  WHITE="3[1;37m"
  BOLD=""
  RESET="3[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/* \(.*\)/$(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\]"

这里的问题是,当您执行 sudo su 时,您正在更改为 root,但您保留了自己的配置文件。该配置文件包含引用 bash 函数的命令提示符设置。但是当你 sudo 到 root 时,你得到的是 root 的 shell,它是 sh 而不是 bash - 所以任何依赖 bash 配置的修改都不会起作用,包括您在 PS1.

中引用的函数

因此,首先要做的是确保您在执行 sudo 时实际上是 运行ning bash 而不是 sh。这很简单——不用 运行ning sudo su,你只需 运行 sudo bash

由于 sudo 默认切换到 root,您现在将 运行将 bash shell 设置为 root,而不是仅仅切换到 root 用户的默认 shell.

如果您仍然遇到问题,这可能是因为您的 .bash_profile 包含对当前用户主目录的引用,因为它在以下行中指向 ~

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

当您 运行 bash 作为您自己时,~ 将扩展到您自己的主目录 - 但是当您 运行 作为 root 时,它将评估为 /var/root 这就是它查找文件的地方。

您可以通过三种方式解决此问题;选择你喜欢的那个。

  1. 更改您的 .bash_profile 以便它包含主目录的完整路径而不仅仅是波浪号
  2. 将所有相关的 bash 文件复制到 /var/root
  3. 代替运行宁sudo su,做sudo su -。这将为您提供 root 的环境而不是您自己的环境。缺点是您将无法使用您自己的所有环境修改,并且您将 运行 宁 sh 而不是 bash。 (在某些操作系统中,它们是同一回事,但在其他操作系统中则不同。我相信 MacOSX 是 shbash 不同的操作系统之一。)

你导出你的 $PS1 了吗? 您可以通过运行命令查看:

printenv

否则你应该通过 运行:

导出它
export -n PS1

之后你可以 运行 sudo 或 sudo su 没有问题