I'm having this command to set prompt in Bash terminal with Git branch name:

export PS1="\w $(git branch | grep '*') "

It shows the correct Git branch name for the first time after running the export command above. However, when checking out another Git branch, the prompt doesn't change to show new Git branch name.

I guess the subbash command $(...) is executed only once, how to make it run every time the prompt is shown after pressing Enter?

2 Answers 2


You need to set PROMPT_COMMAND. First, define a function that will run every time a new prompt is shown:

function setps1()
    PS1="\w $(git branch | grep '*') "

and assign it to PROMPT_COMMAND:


Also check out projects such as liquidprompt.

  • tks, it works!!!
    – Dan D.
    Commented Sep 30, 2020 at 9:26

Set PS1 with a single quote (which will be passed to the prompt) instead of a double quote (which only gets evaluated at creation time). There are rules when using PROMPT_COMMAND and PS1 that I was able to short-circuit learning about when I found a comment mentioning the quote issue.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .