Bash prompt execute command every time a new prompt is displayed

bashprompt

I have the following prompt in bash which shows the current git branch:

PS1+="$(git_prompt)" #git_prompt is a function in my .bashrc

which works when I source the .bashrc, but not when I change the branch, so the PS1 var gets only evaluated when I source the .bashrc, but it should be evaluated every time a new prompt is displayed. How can this be accomplished with bash 4.3 ?

Best Answer

Your problem is that $(git_prompt) is evaluated to some constant string before it is added to $PS1. You have to add the code instead:

PS1+='$(git_prompt)'