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

时间:2017-11-09 21:49:16

标签: bash macos sudo git-bash

我正在运行命令:

sudo bash

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

bash: parse_git_branch: command not found

这是我的.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)

提前致谢。

3 个答案:

答案 0 :(得分:6)

问题是parse_git_branch中定义了.bash_profile,但未导出。当您运行sudo bash时,它会启动一个非登录 shell,其中包含.bashrc而不是.bash_profilePS1已导出,因此在新shell中定义,但parse_git_branch不是。

通常,您要在PS1中定义parse_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

.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

答案 1 :(得分:0)

解决我的问题的方法是在我的{{的开头)添加 hashbang #!/bin/bash-或在您的情况下可能是tcsh) 1}}。

.bashrc顶部没有hashbang,该功能仍适用于PS1,但仅适用于主分区(在我的SSD上),但是当我尝试操纵另一个装载(额外的HDD)上的git存储库时,此消息。

因此,只需添加.bashrc,然后重新加载#!/bin/bash(通过执行.bashrc),就可以解决此问题。

答案 2 :(得分:-1)

通过查看 .bash_profile ,您似乎忘记在parse_git_branch(){}

之前添加关键字 function

尝试更改为,

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

之后,重新加载 .bash_profile ,看看它是否有效。

参考:Functions in Bash

相关问题