bash sh:parse_git_branch:找不到命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33864872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:56:08  来源:igfitidea点击:

sh: parse_git_branch: command not found

gitmacosbashsu

提问by AbhimanyuAryan

I have root enabled on osx El Captain. I tried some of the solution already provided on stackoverflowand supersubut couldn't fix the error. I exported function parse_git_branch()to .bash_profilefrom .bash_promptbut 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.

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

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

.bash_profile

.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

.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\]"

回答by Jenny D

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 shinstead of bash- so any modifications that rely on bash configurations will not work, including the function you are referencing in your PS1.

这里的问题是,当您这样做时sudo su,您正在更改为 root 但您保留了自己的配置文件。该配置文件包含引用 bash 函数的命令提示符设置。但是,当您 sudo 到 root 时,您将获得 root 的 shell,而sh不是bash- 因此任何依赖 bash 配置的修改都将不起作用,包括您在 .bashrc 中引用的函数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.

因此,首先要做的是确保在执行 sudo 时实际运行的是 bash 而不是 sh。这非常简单——您无需运行sudo su,只需运行即可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.

由于 sudo 默认切换到 root,您现在将以 root 身份运行 bash shell,而不仅仅是切换到 root 用户的默认 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:

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

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/rootand that's where it will look for your files.

当您以自己的身份运行 bash 时,~将扩展到您自己的主目录 - 但是当您以 root 身份运行它时,它将评估为/var/root并在那里查找您的文件。

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 shinstead 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 shand bashare different things.)
  1. 更改您的 .bash_profile 使其包含您的主目录的完整路径,而不仅仅是波浪号
  2. 将所有相关的 bash 文件复制到 /var/root
  3. 与其跑步sudo su,不如做sudo su -。这将为您提供 root 的环境而不是您自己的环境。缺点是您自己的所有环境修改都将无法使用,您将运行sh而不是bash. (在某些操作系统中,这些都是一样的东西,但在其他事实并非如此。我认为,MacOSX的是那些之一sh,并bash是不同的东西。)

回答by Walterwhites

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

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

printenv

else you should export it by run:

否则你应该通过运行导出它:

export -n PS1

after you will can run sudo or sudo su without problem

在您可以毫无问题地运行 sudo 或 sudo su 之后