获取错误:bash:parse_git_branch:找不到命令

Isa*_*ina 4 macos bash sudo git-bash

我正在运行命令:

sudo bash
Run Code Online (Sandbox Code Playgroud)

但是我的终端上一直有错误说,

bash: parse_git_branch: command not found
Run Code Online (Sandbox Code Playgroud)

这是我的.bash_profile档案

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

export PS1="\u@\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ "

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM$

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi

export PATH=/usr/local/bin:/Applications/XAMPP/xamppfiles/bin:$PATH
export PATH=/bin:/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:$PATH
export EDITOR='subl -w'



export JAVA_HOME=$(/usr/libexec/java_home)
export JAVA_HOME=$(/usr/libexec/java_home)
export JAVA_HOME=$(/usr/libexec/java_home)
Run Code Online (Sandbox Code Playgroud)

提前致谢.

che*_*ner 7

问题在于parse_git_branch定义.bash_profile,但未导出.当你运行时sudo bash,它启动一个非登录 shell .bashrc而不是.bash_profile.PS1导出,因此在新shell中定义,但parse_git_branch不是.

通常情况下,你会同时定义PS1parse_git_branch.bashrc和导出它们既不.macOS与Linux有点不同,因为终端模拟器启动登录shell而不是普通的交互式shell.一个好的做法是将定义放入.bashrc,然后.bashrc从中获取.bash_profile.

这是我如何拆分你现有的.bash_profile:

.bashrc:

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

PS1="\u@\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ "

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM$

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi
Run Code Online (Sandbox Code Playgroud)

.bash_profile:

# Many of the paths you were adding to PATH should already be
# there in the default configuration; run /usr/lib/path_helper
# to see the default.
PATH=/Applications/XAMPP/xamppfiles/bin:/usr/local/sbin:$PATH
export EDITOR='subl -w'
export JAVA_HOME=$(/usr/libexec/java_home)

[[ -f ~/.bashrc ]] && source ~/.bashrc
Run Code Online (Sandbox Code Playgroud)