Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

git - Why doesn't my bash prompt update?

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 :

RESET="[17]"
NORMAL="[33[0m]"
RED="[33[31;1m]"
YELLOW="[33[33;1m]"
WHITE="[33[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 .bashrc file 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 is easier to understand what's going on if you use the single quotes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...