I'm using this guide to add git branch name to the PS1 variable. I changed it a little bit, to leave my current prompt theme and to add green background to git branch name.

parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'

export PS1="$PS1\e[30;48;5;82m$(parse_git_branch)\e[0m \[\033[00m\]"

Looks like everything works, but when I leave the directory, the git branch name stays. And If I run terminal in not git directory, and cd into git, I can't see the name of branch. But when I open another terminal tab, I see it.

I use Guake terminal with Bash shell.

2 Answers 2


This is happening because your prompt is being set to the value of parse_git_branch when the prompt is set, you need it to run the function each time its run.

You need to put a \ before the $(parse_git_branch) so that the dollar is hidden when the prompt is set so its not run. When the prompt is executed the dollar will then be visible and the command in the brackets will be run thus showing the right value for the folder you are in.

export PS1="$PS1\e[30;48;5;82m\$(parse_git_branch)\e[0m \[\033[00m\]"

This is explaind in the bash docs on the 2nd paragraph of this page http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x279.html

Another useful trick here is to run /bin/bash -x and try it , this shows you much more of what is happening.


I assume you have added the section of code to your ~/.bashrc. This means that the PS1 variable is set only once, and then doesn't change value when you change directories. For this to work, the value of PS1 needs to be re-evaluated after every cd.

One way to solve this is by setting PROMPT_COMMAND to a function which resets PS1. The value of PROMPT_COMMAND is evaluated before every bash prompt (see man bash).

However, an easier way is to install liquidprompt which does what you want, and a lot more.

sudo apt-get install liquidprompt && liquidprompt_activate

You can then configure your dynamic prompt to your heart's content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.