0

Currently my Windows Git Bash shell prompt looks like this: UserName@ComputerName Path (Branch)

When I echo $PS1, I get:
\[\033]0;$MSYSTEM:\w\007\]\n\[\033[32m\]\u@\h \[\033[33m\]\w$(__git_ps1)\[\033[0m\]\n$

In my .bashrc file I tried to update this like so:

PS1="\[\033[32m\]\w$(__git_ps1)\[\033[0m\]\n$"

What happens is that I lose the UserName@ComputerName which is what was intended. However for some reason, the $(__git_ps1) also disappeared.

When I however do it like this: PS1=${PS1:46} it works as expected.

My plan was to add some additional changes (replace a fixed path with nothing) so the substring method is less then optimal.

What's going on?

UPDATE:
When I execute the PS1="\[\033[33m\]\w$(__git_ps1)\[\033[0m\]\n$" in the shell directly, it also works as expected.

Laoujin
  • 9,962
  • 7
  • 42
  • 69

1 Answers1

2

The problem is that due to the double quotes, $(__git_ps1) expands when you define the prompt rather than when the prompt is later drawn.

It works in the shell directly because by defining it again, you cause $(__git_ps1) to be expanded again for the directory you're in. If you change branch, you'll see that it's stuck.

The solution is to use single quotes so that $(__git_ps1) becomes a literal value in the prompt, to be evaluated later:

PS1='\[\033[33m\]\w$(__git_ps1)\[\033[0m\]\n$ '
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • Yaye it works. Why is it required to add an additional space at the end and a `\n` at the front to make it behave **exactly** the same while these were not present with the initial `echo $PS1`? – Laoujin May 17 '14 at 22:05
  • `echo "$PS1"`, quoted, means "Write out the value of `$PS1`". `echo $PS1`, unquoted, means "ruin all the whitespace and trash special characters". It's very counter-intuitive, I know. You can use `printf "PS1=%q\n" "$PS1"` to print out `$PS1` in a way that you can reuse as input. – that other guy May 17 '14 at 22:10