3

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

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

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

6 Answers 6

12

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".

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

    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
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I had the same problem and for me it did work perfectly, thanks
Don't export PS1 (just like this answer) and your problems will be solved.
9

If your parse_git_branch is defined in ~/.bash_profile, it will not be loaded when you open a non-login shell (e.g. by running 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_profile upon startup, whereas non-login shells (e.g. that when you run bash from within Terminal) do not.

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

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

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

Comments

4

Instead of having

parse_git_branch

call in PS1 definition alone you may use

parse_git_branch 2>/dev/null

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

Comments

2

have you export your $PS1 ? You can check by run command:

printenv

else you should export it by run:

export -n PS1

after you will can run sudo or sudo su without problem

Comments

2

In .bashrc add

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

Comments

1

The key to this is to NOT export PS1. If it's exported, then any non-login shell also takes PS1. Since .bash_profile is automatically source'd by the login shell, the PS1 variable only affects the login shell.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.