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
sh: parse_git_branch: command not found
提问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_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.
我在 osx El Captain 上启用了 root。我尝试了一些已经在stackoverflow和supersu上提供的解决方案,但无法修复错误。我导出function parse_git_branch()
到.bash_profile
from.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 sh
instead 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/root
and 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.
您可以通过三种方法解决此问题;选择您喜欢的任何一种。
- 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.)
- 更改您的 .bash_profile 使其包含您的主目录的完整路径,而不仅仅是波浪号
- 将所有相关的 bash 文件复制到
/var/root
- 与其跑步
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 之后