Getting error: bash: parse_git_branch: command not found
Asked Answered
N

4

9

I am running the command:

sudo bash

But I keep getting an error on my terminal that says,

bash: parse_git_branch: command not found

Here is my .bash_profile file

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

export PS1="\u@\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ "

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM$

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi

export PATH=/usr/local/bin:/Applications/XAMPP/xamppfiles/bin:$PATH
export PATH=/bin:/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:$PATH
export EDITOR='subl -w'



export JAVA_HOME=$(/usr/libexec/java_home)
export JAVA_HOME=$(/usr/libexec/java_home)
export JAVA_HOME=$(/usr/libexec/java_home)

Thanks in advance.

Neukam answered 9/11, 2017 at 21:49 Comment(1)
This is not related to your question, but you can do git branch --show-current.Quisling
N
12

The problem is that parse_git_branch is defined in .bash_profile, but not exported. When you run sudo bash, it starts an nonlogin shell that sources .bashrc instead of .bash_profile. PS1 was exported and so is defined in the new shell, but parse_git_branch is not.

Typically, you would define both PS1 and parse_git_branch in .bashrc and export neither of them. macOS is a little different from Linux, in that a terminal emulator starts a login shell instead of an ordinary interactive shell. A good practice is to put the definitions in .bashrc, then source .bashrc from .bash_profile.

Here's how I would split up your existing .bash_profile:

In .bashrc:

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

PS1="\u@\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ "

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM$

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi

In .bash_profile:

# Many of the paths you were adding to PATH should already be
# there in the default configuration; run /usr/lib/path_helper
# to see the default.
PATH=/Applications/XAMPP/xamppfiles/bin:/usr/local/sbin:$PATH
export EDITOR='subl -w'
export JAVA_HOME=$(/usr/libexec/java_home)

[[ -f ~/.bashrc ]] && source ~/.bashrc
Nievesniflheim answered 9/11, 2017 at 22:27 Comment(0)
T
0

What fixed the issue in my case was adding the hashbang (#!/bin/bash - or could be tcsh in the end in your case) to the beginning of my .bashrc.

Without hashbang at the top of .bashrc the function still applied to PS1 but only within the main partition (on my SSD), but when I tried to manipulate git repositories on another mount (an extra HDD) I had this message.

So just adding #!/bin/bash and then reloading .bashrc (by doing source ~.bashrc) fixed this issue for me.

Threadgill answered 12/4, 2019 at 20:54 Comment(0)
P
0

Restart your terminal and read the startup logs - initial lines. It should log which file has issues and what is wrong.

Pernod answered 4/12, 2023 at 4:25 Comment(0)
S
-2

By looking at your .bash_profile, it seems you have forgotten to add the keyword function before parse_git_branch() {}

Try changing to,

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

After that, reload your .bash_profile and see if it works.

Reference: Functions in Bash.

Shipwreck answered 9/11, 2017 at 21:57 Comment(5)
Hello, mattias. Unfortunately it didn't work. I keep getting the same error.Neukam
function is an optional bash extension; its use is not even recommended.Nievesniflheim
(...and its habit of advising/showcasing/demonstrating things that are considered bad practice is part of why linking to the ABS is frowned on).Lylelyles
@CharlesDuffy What is ABS an abbreviation for?Shipwreck
Heh -- the other common bash guide by TLDP, the "Advanced" Bash Scripting guide. I see you linked into the introductory one -- apologies about the confusion there; that said, the criticism generally applies. BTW, in terms of bash references/resources that are worth recommending -- see the Wooledge BashGuide, the Bash Hackers' Wiki, and of course the official manual.Lylelyles

© 2022 - 2025 — McMap. All rights reserved.