Git branch name in prompt

 ̄綄美尐妖づ 提交于 2019-12-05 03:28:20
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_profile to .bashrc.

When you open your terminal, .bash_profile is executed and therefore PS1 is defined. Then you execute screen, and screen reads the environment variable PS1 which includes a call to parse_git_branch and tries to parse it. But, since screen didn't execute .bash_profile the function parse_git_branch is not defined inside screen.

Move the definition of PS1 to .bashrc because both, screen and iTerm execute it.

Gilad Halevy

This is much simpler and avoids the unnecessary 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"

}

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

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

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

I had the same error in OS X High Sierra when switching to root or when starting to ssh-agent /bin/bash I resolved it to put it in /etc/bashrc with check if i am 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/* \(.*\)/ (\1)/'
        }
        PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
fi
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!