Prompt doesn't update git branch

I tried to integrate the current git branch in my prompt, but it doesn't behave as expected. I'm using bash on ubuntu 16.04 and git 2.7.4. When I start a terminal nothing of git is shown. If I source my .bashrc from inside a repository the branch is shown, but doesn't update anymore. This is what I wrote in my .bashrc:

green="\[\033[01;32m\]"
blue="\[\033[01;34m\]"
no_color="\[\033[00m\]"
purple="\[\033[01;35m\]"

source ~/.git-prompt.sh
export PS1="$purple\u $green$(__git_ps1 " (%s)") $blue\W $no_color \$ "

Update:

I tried follow the instructions in git-prompt but still the same result. However if I just copy the suggestion from git-prompt: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ' it works, but of course doesn't look like I want. Is there some syntax error I'm missing? The template PS1 works even if don't use export, just PS1=...


Ok I found the solution. It is not necessary to use prompt command. The bug arises due to a syntax error.

If I assign the content of PS1 with "" in order to use my variables for the colors, it only executes the __git_ps1 function when .bashrc is sourced.

But when I assign the PS1 content within '' and do it without variables for the color and instead write the codes out, it works as expected. And as I read here it seems to be better practice not to export PS1 to the environment.

So the solution looks like:

PS1='\[\033[01;35m\]\u \[\033[01;32m\]$(__git_ps1 " (%s)") \[\033[01;34m\]\W \[\033[00m\] \$ '

However I would be curious to know, what's the reason for this. It works within "" in macOS.


That's because .bashrc is read only once: when you start a new interactive non-login shell (when you open a new terminal, for example). So the PS1 is set the first time you open the terminal and is never changed.

The right way of doing this (assuming your git-prompt and __git_ps scripts work as expected) is to use PROMPT_COMMAND. As explained in man bash:

   PROMPT_COMMAND
          If set, the value is executed as a command prior to issuing each
          primary prompt.

You want __git_ps1 to be run before each prompt is shown (so after every command you run; after a cd, for instance). Therefore, you need to put it into PROMPT_COMMAND. If your __git_ps1 is what I think it is, it will be setting PS1 for you. You just need to run it each time a prompt is shown. So, add this line to your ~/.bashrc:

PROMPT_COMMAND="$purple\u $green$(__git_ps1 "(%s)") $blue\W $no_color \$ "

Finally, I can't be sure since you're not showing the relevant scripts, but I very much doubt that the (%s) makes sense there. That will just print the string %s. If you need more help, please edit your question and i) include both __git_ps1 and /etc/bash_completion.d/git-prompt (or tell us how you installed them if they came from an Ubuntu package) and ii) show us what your prompt is supposed to look like.


Ran into the same problem. You can simply escape the $(...) to \$(...)

export PS1="$purple\u $green\$(__git_ps1 " (%s)") $blue\W $no_color \$ "