4

I am trying to set my user prompt in the Git for Windows Bash prompt. Here's a (very simple) prompt that works:

# \@                 Prints the time: 11:14 AM
# \w                 Prints the full cwd path
# $(__git_ps1 ...)   Prints the current git branch

PS1='\n[\@] \w $(__git_ps1 "(%s)") \$ '

As soon as I add a second newline character into the mix, I get the following error:

 bash: command substitution: line 1: syntax error near unexpected token ')'
 bash: command substitution: line 1: `__git_ps1 "(%s)")'

The variant of the prompt that throws this error is (note the newline before the $ prompt):

PS1='\n[\@] \w $(__git_ps1 "(%s)") \n\$ '

I'm thinking there must be an issue with the __git_ps1 routine that's causing this issue to occur, but I don't really understand it. Is there something simple here that I'm missing?

Jonah Bishop
  • 12,279
  • 6
  • 49
  • 74

1 Answers1

7

So, it turns out that this has already been answered.

By using quoted string expansion, one can avoid the problem:

PS1='\n[\@] \w $(__git_ps1 "(%s)")'$'\n\$ '

It's amazing to me that in 2016 Bash still uses this terrible, terrible syntax. There has to be a better way.

Jonah Bishop
  • 12,279
  • 6
  • 49
  • 74
  • the difference is the `'$'` near the end, refer to https://www.diffchecker.com/qq8HJsjy – TT-- Nov 05 '18 at 17:27