【问题标题】:Getting error: bash: parse_git_branch: command not found出现错误:bash:parse_git_branch:找不到命令
【发布时间】:2018-04-23 01:19:28
【问题描述】:

我正在运行命令:

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)

提前致谢。

【问题讨论】:

  • 这与你的问题无关,但你可以做git branch --show-current

标签: bash macos sudo git-bash


【解决方案1】:

问题是parse_git_branch.bash_profile中定义,但没有导出。当您运行sudo bash 时,它会启动一个nonlogin shell,它提供.bashrc 而不是.bash_profilePS1 已导出,因此在新 shell 中定义,但 parse_git_branch 没有。

通常,您会在 .bashrc 中同时定义 PS1parse_git_branch 并且都不导出它们。 macOS 与 Linux 略有不同,终端模拟器启动登录 shell 而不是普通的交互式 shell。一个好的做法是将定义放在.bashrc 中,然后从.bash_profile 获取.bashrc

以下是我将如何拆分您现有的.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

【讨论】:

    【解决方案2】:

    在我的情况下解决问题的方法是将 hashbang#!/bin/bash - 或者在你的情况下最终可能是tcsh)添加到我的.bashrc 的开头。

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

    所以只需添加#!/bin/bash,然后重新加载.bashrc(通过执行source ~.bashrc)为我解决了这个问题。

    【讨论】:

      【解决方案3】:

      通过查看您的 .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

      【讨论】:

      • 你好,马蒂亚斯。不幸的是,它没有用。我不断收到同样的错误。
      • function 是可选的bash 扩展;甚至不推荐使用它。
      • (......以及它建议/展示/演示被认为是不良做法的事情的习惯是不赞成与 ABS 链接的部分原因)。
      • @CharlesDuffy ABS 是什么的缩写?
      • 呵呵——TLDP 的 other 通用 bash 指南,“高级”Bash 脚本指南。我看到你链接到介绍性的 - 为那里的混乱道歉;也就是说,批评普遍适用。顺便说一句,就值得推荐的 bash 参考/资源而言——请参阅Wooledge BashGuideBash Hackers' Wiki,当然还有the official manual
      猜你喜欢
      • 2016-06-23
      • 2021-04-26
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 2022-01-16
      • 2019-08-17
      相关资源
      最近更新 更多