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?

1 Answer 1


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!!!
    – Dee
    Sep 30, 2020 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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