为什么我的 bash 不提示更新?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5379986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:38:36  来源:igfitidea点击:

Why doesn't my bash prompt update?

gitbashps1

提问by greg0ire

I'm new to git and I'm trying to add the current git branch to my already existing prompt, which is defined as follows :

我是 git 新手,我正在尝试将当前的 git 分支添加到我已经存在的提示中,其定义如下:

RESET="\[7\]"
NORMAL="\[3[0m\]"
RED="\[3[31;1m\]"
YELLOW="\[3[33;1m\]"
WHITE="\[3[37;1m\]"
SMILEY="${WHITE}:)${NORMAL}"
FROWNY="${RED}:(${NORMAL}"
SELECT="if [ $? = 0 ]; then echo \"${SMILEY}\"; else echo \"${FROWNY}\"; fi"

export PS1="${RESET}${YELLOW}\u@\h${NORMAL} \`${SELECT}\` ${YELLOW}\w $(__git_ps1) >${NORMAL} "

I tried it (by sourcing my .bashrcfile again) and it seemed to work, but then I went on another branch and it did not update. How can I make sure the $(__git_ps1)is not cached?

我尝试了它(通过.bashrc再次获取我的文件)并且它似乎工作,但后来我去了另一个分支并且它没有更新。如何确保$(__git_ps1)未缓存?

回答by geekosaur

You need a backslash on the $so it isn't expanded immediately. (Compare to the `...`, which is a different way of writing $(...).)

你需要一个反斜杠,$所以它不会立即扩展。(与 相比`...`,这是一种不同的写作方式$(...)。)

export PS1="${RESET}${YELLOW}\u@\h${NORMAL} \`${SELECT}\` ${YELLOW}\w $(__git_ps1) >${NORMAL} "

I would agree with @MikeSep about using single quotes, but it's actually a bit more optimal to let the colors and such be substituted immediately. Not necessary, just somewhat better. That said, it iseasier to understand what's going on if you use the single quotes.

我同意@MikeSep 关于使用单引号的看法,但实际上让颜色等立即替换更佳。没必要,稍微好一点。这就是说,它更容易理解发生了什么事情,如果你使用单引号。

回答by Mike Seplowitz

Your PS1string is probably getting evaluated before it is getting saved, but you really want the __git_ps1command to run each time you get a command prompt. I'd recommend using single quotes instead of double quotes for your export PS1='${RESET}...'line.

您的PS1字符串可能会在保存之前进行评估,但您确实希望__git_ps1每次收到命令提示符时都运行该命令。我建议对您的export PS1='${RESET}...'行使用单引号而不是双引号。