98

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="\[\017\]"
NORMAL="\[\033[0m\]"
RED="\[\033[31;1m\]"
YELLOW="\[\033[33;1m\]"
WHITE="\[\033[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?

3
  • 15
    I love the idea of having :) and :( in your prompt depending on whether the last command failed :) Commented Mar 21, 2011 at 15:51
  • @Mark Longair : I found it here : serverfault.com/questions/4889/… BTW, why did you delete your solution, it works too!
    – greg0ire
    Commented Mar 21, 2011 at 15:54
  • it was minutes later than the other two, and is less neat than @geekosaur's anyway Commented Mar 21, 2011 at 15:59

2 Answers 2

154

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.

5
  • 2
    Also, if you ever work in things other than terminal programs directly (Emacs/Vim command buffers, screen, etc.), you might want to look into using tput instead of hard-coded color escapes.
    – geekosaur
    Commented Mar 21, 2011 at 15:49
  • 1
    It is necessary to have the colour variables be substituted in this case, I think - at least, I just get the escape codes with @MikeSep's suggestion, whereas your one works fine. (+1) Commented Mar 21, 2011 at 15:50
  • @Mark: That's interesting; the ${SELECT} one demonstrates that recursive evaluation is being done. Maybe it's only for commands? I generally use a PS1 function instead because it's easier to figure out what's going on.
    – geekosaur
    Commented Mar 21, 2011 at 15:54
  • 3
    man, you are a life savior. I have a question though: I have a MBP and an iMac. Same .bash_profile, same version of the bash (installed via homebrew). on the MBP if I don't add the \ in front of the $(__git_ps1), it won't be evaluated. If I do the same thing on the iMac, it gets evaluated even without the \. Any idea how this can happen?
    – molli
    Commented Oct 10, 2014 at 9:29
  • @molli could you let us know which versions of macOS/ OS X are running on your MBP and iMac? the same ones?
    – esaruoho
    Commented Nov 9, 2018 at 7:51
32

Your PS1 string is probably getting evaluated before it is getting saved, but you really want the __git_ps1 command 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.

3
  • may work, but I'm having problems converting all the system to single quotes, so I'll you use geekosaur's solution. But you get my upvote too.
    – greg0ire
    Commented Mar 21, 2011 at 16:02
  • Ah, yes. His is better -- I missed the local vars for colors. Thanks for the vote anyway. :) Commented Mar 21, 2011 at 16:50
  • putting simple quote instead of double quote saved me, thx :)
    – Banane
    Commented Dec 6, 2022 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.