0

目前,如果我从已经包含 git 项目的文件夹启动 bash,则此方法有效。

PS1="${debian_chroot:+($debian_chroot)}\[$BPurple\]\u\[$BGreen\]@\[$BCyan\]\h\[$BGreen\]: \[$BBlue\]\w\[$Yellow\]`__git_ps1`\[$Green\] \$ \[$White\]"

我的问题是,如果我cd进入一个 git 文件夹,什么也不会发生。然后,如果我从 git repo 开始,((master)确实出现了,如果我cd到其他任何地方,(master)仍然出现在我的提示中。

所以,PS1变量没有更新。我该如何解决?

4

2 回答 2

0

Try this:

PS1='${debian_chroot:+($debian_chroot)}[$BPurple]\u[$BGreen]@[$BCyan]\h[$BGreen]: [$BBlue]\w[$Yellow]$(__git_ps1)[$Green] \$ [$White]'

I simply replaced the double quotes with single quotes, and wrapped the call to __git_ps1 in a command substitution. If that doesn't work (I haven't included parameter expansion in my prompt for a while), try

PS1="..."'$(__git_ps1)'"..."

where the first '...' is everything prior to '__git_ps1', and the second '...' is everything after. Basically, three strings concatenated together, with the first and third in double quotes and the second in single quotes.

于 2012-07-19T14:29:58.293 回答
0

尝试在几乎使用变量的地方用简单引号替换双引号

PS1='${debian_chroot:+($debian_chroot)}'"[$BPurple]\u[$BGreen]@[$BCyan]\h[$BGreen]: [$BBlue]\w[$Yellow]__git_ps1[$Green] \$ [$White]"
于 2012-07-19T13:03:15.433 回答