bash 提示中的 Git 分支名称

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

Git branch name in prompt

bashgitgnu-screen

提问by user2610733

I am able to show the git branch name in the shell prompt. But whenever I am using screen I am getting

我能够在 shell 提示中显示 git 分支名称。但是每当我使用屏幕时,我都会得到

bash: parse_git_branch: command not found

and git branch is not shown. Please help me get this in the screen sessions also.

并且没有显示 git branch。请帮助我在屏幕会话中也得到这个。

I have following in my .bash_profile.

我的 .bash_profile 中有以下内容。

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

export PS1="[\W$(parse_git_branch)]$ "

I don't have .git-completion.bash

我没有 .git-completion.bash

System specs:

系统规格:

  • OS: OSX 10.8.4
  • Terminal & iTerm2
  • Screen version: 4.00.03 (FAU) 23-Oct-06
  • 操作系统:OSX 10.8.4
  • 终端 & iTerm2
  • 屏幕版本:4.00.03 (FAU) 23-Oct-06

回答by koomie

I had the same issue when running under screen and was able to resolve by moving the definition of the parse_git_branch()function from .bash_profileto .bashrc.

在屏幕上运行,当我有同样的问题,并能够通过移动的定义,以解决parse_git_branch()从功能.bash_profile.bashrc

回答by Simon Soriano

When you open your terminal, .bash_profileis executed and therefore PS1is defined. Then you execute screen, and screen reads the environment variable PS1which includes a call to parse_git_branchand tries to parse it. But, since screen didn't execute .bash_profilethe function parse_git_branchis not defined inside screen.

当您打开终端时,会.bash_profile被执行并因此PS1被定义。然后执行 screen,screen 读取环境变量PS1,其中包括parse_git_branch对它的调用并尝试解析它。但是,由于 screen 没有执行.bash_profile该函数parse_git_branch在 screen 中没有定义。

Move the definition of PS1to .bashrcbecause both, screen and iTerm execute it.

移动PS1to的定义,.bashrc因为 screen 和 iTerm 都执行它。

回答by Gilad Halevy

This is much simpler and avoids the unnecessary sed:

这要简单得多,并且避免了不必要的 sed:

parse_git_branch () {

    while read -r branch; do
        [[ $branch = \** ]] && current_branch=${branch#* }
    done < <(git branch 2>/dev/null)

    [[ $current_branch ]] && printf ' [%s]' "$current_branch"

}

回答by Mazel Tov

I had the same error in OS X High Sierra when switching to root or when starting to ssh-agent /bin/bashI resolved it to put it in /etc/bashrcwith check if i am root

切换到 root 或开始时,ssh-agent /bin/bash我在 OS X High Sierra 中遇到了同样的错误我解决了它/etc/bashrc并检查我是否是 root

if [[ $UID == 0 ]]; then
        PS1="\[\e[1;31;40m\]\u@\h \W\[\e[0m\]$ "
else
        parse_git_branch() {
                git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ ()/'
        }
        PS1="\u@\h \[3[32m\]\w\[3[33m\]$(parse_git_branch)\[3[00m\] $ "
fi

回答by Nick Tomlin

You are missing a 'at the end of your sed statement:

'在 sed 语句的末尾缺少一个:

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ ()/'
}
export PS1="[\W$(parse_git_branch)]$ "

Othewerise, it seems to work for me in bash-3.2

Othewerise,它似乎对我有用 bash-3.2