bash:parse_git_branch:找不到命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35881412/
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 14:21:27  来源:igfitidea点击:

bash: parse_git_branch: command not found

bashterminalosx-elcapitan

提问by jtaylor94

This should be very simple. I recently noticed that when I type 'bash' into Terminal on Mac it shows this:

这应该很简单。我最近注意到,当我在 Mac 上的终端中键入“bash”时,它会显示以下内容:

Jays-MacBook-Pro: ~ $ bash
bash: parse_git_branch: command not found

When before it didn't. Can someone explain why and how to resolve.

以前没有的时候。有人可以解释为什么以及如何解决。

回答by Lungang Fang

It is likely that you configured BASH to run parse_git_branch and print the result as part of PS1 (or alike). You can check this by: "echo $PS1" and "echo $PROMPT_COMMAND".

您可能将 BASH 配置为运行 parse_git_branch 并将结果打印为 PS1(或类似的)的一部分。您可以通过以下方式检查:“echo $PS1”和“echo $PROMPT_COMMAND”。

However, parse_git_branch is not a builtin function of bash. Below is how I configured my PS1. You may want to copy my git_branch_4_ps1 as your parse_git_branch

但是, parse_git_branch 不是 bash 的内置函数。下面是我如何配置我的 PS1。你可能想复制我的 git_branch_4_ps1 作为你的 parse_git_branch

    PS1='\n'                           # begin with a newline
    PS1=$PS1'\[\e[38;5;101m\]\! \t '   # time and command history number
    PS1=$PS1'\[\e[38;5;106m\]\u@\h '   # user@host
    PS1=$PS1'\[\e[7;35m\]${MY_WARN}\[\e[0m\] ' # warning message if there is any
    PS1=$PS1'\[\e[38;5;10m\]${MY_EXTRA} '      # extra info if there is any
    PS1=$PS1'\[\e[0;36m\]$(git_branch_4_ps1) ' # git_branch_4_ps1 defined below
    PS1=$PS1'\[\e[38;5;33m\]\w'        # working directory
    PS1=$PS1'\n\[\e[32m\]$ '          # "$"/"#" sign on a new line
    PS1=$PS1'\[\e[0m\]'                # restore to default color

    function git_branch_4_ps1 {     # get git branch of pwd
        local branch="$(git branch 2>/dev/null | grep "\*" | colrm 1 2)"
        if [ -n "$branch" ]; then
            echo "(git: $branch)"
        fi
    }

回答by andrej

Instead of having

而不是拥有

parse_git_branch

call in PS1 definition alone you may use

单独调用 PS1 定义,您可以使用

parse_git_branch 2>/dev/null

to send stderrto /dev/null. This will silence the error you don't want to see.

发送stderr/dev/null. 这将使您不想看到的错误静音。

回答by orthocresol

If your parse_git_branchis defined in ~/.bash_profile, it will not be loaded when you open a non-login shell (e.g. by running bash).

如果您parse_git_branch在 中定义~/.bash_profile,则在您打开非登录外壳程序(例如通过运行bash)时将不会加载它。

The differences between login and non-login shells are described here: Difference between Login Shell and Non-Login Shell?For our purposes, the main difference is that login shells (e.g. that when you first open Terminal) automatically source ~/.bash_profileupon startup, whereas non-login shells (e.g. that when you run bashfrom within Terminal) do not.

登录shell和非登录shell的区别描述如下:Difference between Login Shell and Non-Login Shell? 就我们的目的而言,主要区别在于登录外壳(例如,当您第一次打开终端时)~/.bash_profile在启动时自动获取,而非登录外壳(例如,当您bash从终端中运行时)不会。

To fix this error, simply source your ~/.bash_profileafter running bash:

要修复此错误,只需~/.bash_profile在运行 bash 后获取源代码:

user@host:~ $ bash
bash: parse_git_branch: command not found
user@host:~ $ source .bash_profile

Alternatively, place the function in ~/.bashrcinstead, which will be automatically sourced by non-login shells (as covered in the earlier link).

或者,将函数放入~/.bashrc,它将由非登录 shell 自动获取(如前面的链接中所述)。

回答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 之后