为什么PS1中的换行符会在Git for Windows bash中引发语法错误? [英] Why does a newline in PS1 throw a syntax error in Git for Windows bash?
问题描述
我正在尝试在 Git for Windows Bash提示中设置用户提示.这是一个(非常简单的)提示,可以正常工作:
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\$ '
我认为__git_ps1
例程一定存在一个导致此问题发生的问题,但我并不十分了解.这里有我想念的简单东西吗?
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?
推荐答案
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\$ '
令我惊讶的是,2016年Bash仍然使用这种可怕的语法.必须有更好的方法.
It's amazing to me that in 2016 Bash still uses this terrible, terrible syntax. There has to be a better way.
这篇关于为什么PS1中的换行符会在Git for Windows bash中引发语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!