1

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

3

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:

PROMPT_COMMAND=setps1

Also check out projects such as liquidprompt.

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

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.

https://stackoverflow.com/questions/38429007/why-ps1-is-not-updated-with-the-git-branch-name

You must log in to answer this question.

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