My PS1 in my ~/.bash_profile:

export PS1="\\n\[\033[38;5;246m\]\u@\[\033[38;5;245m\]\h\[\033[38;5;15m\] \[\033[38;5;28m\]\w\[\033[38;5;15m\]\[\033[38;5;2m\]`__git_ps1`\[\033[38;5;15m\] \[\033[38;5;90m\]\t\[\033[38;5;15m\] \[\033[38;5;232m\]\[\033[38;5;15m\] \n\[\033[38;5;0m\]\\$ "

(sorry, I don't have any aliases for my color codes, I created this prompt with an online editor)

Which is a bit messy but produces a very nice prompt: enter image description here

But the current branch displayed is always wrong if I switch

wrong branch

I'm not sure why this would happen. If I run the command by itself, I get the correct value.

$ echo `__git_ps1`

If I source the .bash_profile the new value will come in (but will be wrong next time I switch). Am I doing something wrong?

  • Use single quotes around the assignment (or just the __git_ps1 part so you don't need to fix escapes all through the prompt) so it isn't called/evaluated immediately and is left unexpanded for the shell to call at prompt display time. – Etan Reisner Nov 26 '15 at 3:24
export PS1="…`__git_ps1`…"

With `__git_ps1` inside double quotes, this command runs the command __git_ps1 and assigns its output (and other surrounding text) to the variable PS1. Thus your prompt is showing the branch that was determined when your .bash_profile was executed.

You need to run __git_ps1 each time bash displays a prompt. (Actually you don't need to run it again until the git information has changed, but that's difficult to detect.) There are two ways to do that.

  • Include the literal text `__git_ps1` in the PS1 variable. Make sure that bash is configured to perform shell expansions on the prompt string, with the promptvars option turned on; that's the case by default but it can be turned off with shopt -u promptvars.

    PS1='\n\[…\]$(__git_ps1)\[…\]\$ '
  • Update the prompt content by a command run from the PROMPT_COMMAND variable.

    update_PS1 () {
      PS1="\\n\\[…\\]$(__git_ps1)\[…\]\\$ "
    shopt -u promptvars

By the way, the prompt is a shell configuration, not a global setting, so you should set it in ~/.bashrc, not in ~/.bash_profile.

  • 1
    First option worked like a charm, thanks! – The Godfather Aug 7 '18 at 14:22

It's just a simple matter of quoting. Change `__git_ps1` to \$(__git_ps1), or, if you must use backticks: \`__git_ps1\`.

To convince yourself just change your PS1 to (open a new shell instance if you want to get back cleanly to your previous setup):

$ PS1="$(date) >"
Thu Nov 26 20:02:34 EST 2015 >_

The only problem is that it will not update (wait some seconds to press enter).

But this will:

$ PS1="\$(date) >"
Thu Nov 26 20:06:20 EST 2015
Thu Nov 26 20:06:25 EST 2015

That's all. write exit. (update your prompt) Get back to work.



Notice down at the bottom they don't set:

export PS1="\n... `__git_ps1 ...

The set it to this sick ass piece of work:

export PS1=$IBlack$Time12h$Color_Off'$(git branch &>/dev/null;\

if [ $? -eq 0 ]; then \
  echo "$(echo `git status` | grep "nothing to commit" > /dev/null 2>&1; \
  if [ "$?" -eq "0" ]; then \
    # @4 - Clean repository - nothing to commit
    echo "'$Green'"$(__git_ps1 " (%s)"); \
  else \
    # @5 - Changes to working tree
    echo "'$IRed'"$(__git_ps1 " {%s}"); \
  fi) '$BYellow$PathShort$Color_Off'\$ "; \
else \
  # @2 - Prompt when not in GIT repo
  echo " '$Yellow$PathShort$Color_Off'\$ "; \

The relevant bits of which are "$(__git_ps1 " (%s)" or "$(__git_ps1 " {%s}"

  • The relevant bits of which are actually the single quotes. – Etan Reisner Nov 26 '15 at 3:23
  • And actually many things about that snippet are sub-par at best. – Etan Reisner Nov 26 '15 at 3:27

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.