Below is what I currently have in the .bashrc file located in my home directory.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/';
}
PS1='\n' # New line
PS1=$PS1'\[\033[01;32m\]\u' # Username
PS1=$PS1' \[\033[00m\]at \[\033[01;35m\]\h' # at Hostname
PS1=$PS1' \[\033[00m\]in \[\033[01;36m\]\W' # in Directory
PS1=$PS1'\[\033[01;33m\]\$(parse_git_branch)' # Git branch
PS1=$PS1'\[\033[00m\]\n\$ ' # New line and prompt
export PS1
This bash prompt should be
user at host in ~ (main)
$
when on the main branch of a git repository in my home directory. Instead, the bash prompt is
user at host in ~$(parse_git_branch)
$
The function in the PS1 definition is just showing up as text in the prompt rather than being evaluated.
Does anyone have any insight into why this occurring?
EDIT:
When changing the line calling the function to use double quotes and removing the slash before the call like below,
PS1=$PS1"\[\033[01;33m\]$(parse_git_branch)"
the git branch shows correctly when first opening bash but doesn't update. If I change the line calling the function to still use double quotes but include the slash or using single quotes without the slash like either below,
PS1=$PS1"\[\033[01;33m\]\$(parse_git_branch)"
PS1=$PS1'\[\033[01;33m\]$(parse_git_branch)'
I get the following error when opening bash,
$ bash
bash: command substitution: line 1: syntax error near unexpected token `)'
bash: command substitution: line 1: `parse_git_branch)'
EDIT2:
Problem was fixed by changing the line calling the function to
PS1=$PS1'\[\033[01;31m\]`parse_git_branch`'
as shown here.