为什么 PS1 中的换行符会在 Git for Windows bash 中引发语法错误?

Jon*_*hop 4 windows git bash

我正在尝试在Git for Windows Bash 提示中设置我的用户提示。这是一个有效的(非常简单的)提示:

# \@                 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)") \$ '
Run Code Online (Sandbox Code Playgroud)

一旦我在混合中添加第二个换行符,我就会收到以下错误:

 bash: command substitution: line 1: syntax error near unexpected token ')'
 bash: command substitution: line 1: `__git_ps1 "(%s)")'
Run Code Online (Sandbox Code Playgroud)

引发此错误的提示的变体是(注意$提示前的换行符):

PS1='\n[\@] \w $(__git_ps1 "(%s)") \n\$ '
Run Code Online (Sandbox Code Playgroud)

我__git_ps1认为导致此问题发生的例程一定存在问题,但我真的不明白。这里有什么简单的东西我错过了吗?

Jon*_*hop 5

所以,事实证明这已经得到了回答。

通过使用带引号的字符串扩展,可以避免这个问题:

PS1='\n[\@] \w $(__git_ps1 "(%s)")'$'\n\$ '
Run Code Online (Sandbox Code Playgroud)

令我惊讶的是,在 2016 年 Bash 仍然使用这种可怕的、可怕的语法。一定有更好的方法。