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=...
Best Answer
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:
However I would be curious to know, what's the reason for this. It works within
""
in macOS.