1

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.

torek
  • 448,244
  • 59
  • 642
  • 775
Kistler21
  • 11
  • 2
  • Does [this](https://askubuntu.com/questions/651871/why-is-my-function-not-re-evaluated-in-ps1) help? – Silidrone Feb 13 '21 at 18:25
  • 2
    You are trying two extremes: double escaping the `$` by including it in single quotes and prefixing with a backslash, and not escaping it at all inside double quotes. It needs to be escaped *once*: either in single quotes without the additional backslash, or in double quotes with the backslash. (The error in your edit comes from something else; what, it isn't clear.) – chepner Feb 13 '21 at 18:50
  • You also don't need to export `PS1`; any shell that cares about its value is already sourcing `.bashrc`. – chepner Feb 13 '21 at 18:51
  • The error on line one suggests a problem defining `parse_git_branch` in the first place, not your attempt to use it in the prompt. – chepner Feb 13 '21 at 18:53
  • @Michael Kistler: if you find the answer to your own question, you can post it as an answer (and select it as the correct answer!) instead of editing your question – jidicula Feb 13 '21 at 23:39
  • This has nothing to do with Git ("git bash" is a version of bash ported to Windows, and should probably be called "windows bash" to be less misleading). I snipped the [tag:git] tag. – torek Feb 14 '21 at 01:16

0 Answers0