10

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

bash: parse_git_branch: command not found

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

I have following in my .bash_profile.

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

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

I don't have .git-completion.bash

System specs:

  • OS: OSX 10.8.4
  • Terminal & iTerm2
  • Screen version: 4.00.03 (FAU) 23-Oct-06
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user2610733
  • 101
  • 1
  • 4

5 Answers5

5

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.

khagler
  • 3,996
  • 29
  • 40
koomie
  • 51
  • 1
  • 1
5

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.

Simon Soriano
  • 803
  • 12
  • 19
3

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"

}
jfunez
  • 397
  • 6
  • 23
2

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

Nick Tomlin
  • 28,402
  • 11
  • 61
  • 90
  • unfortunately it is not working for me. Do I have to add something in the screenrc too? – user2610733 Jul 26 '13 at 17:29
  • are you still getting the "command not found" message? What else do you have in your .bash_profile? For fun, try putting a test function in your ``.bash_profile`` `` testFunc () { echo "test!" }`` and calling it. – Nick Tomlin Jul 26 '13 at 18:16
  • 4
    I resolved it. once I am in screen I use 'source ~/.bash_profile'. It brings all the settings back. Thanks for the help. – user2610733 Jul 27 '13 at 20:13
  • 1
    I have the need in my mac to run this AND my .bashrc file otherwise after a restart they are not applied. I will try and keep these noted somewhere...thanks for everyone contributing :) – landed May 11 '15 at 14:27
1

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
Mazel Tov
  • 2,064
  • 14
  • 26