1

After searching the net, I found that one can display the git branch name with the PS1 with below

PS1="$(__git_ps1 ) "

While this is working fine, and it display the branch name in PS1 prompt. But it is displayed where-ever I navigate, even outside the git-checked-out-repo.

I would like to display the branch name only if I am under a git tracked folder/repo. Any way to achieve this? Searching SO and net didn't pop-up helpful links in this respect.

mtk
  • 13,221
  • 16
  • 72
  • 112

1 Answers1

3

You need to defer execution of $(__git_ps1) until PS1 is actually displayed. Use single quotes:

PS1='$(__git_ps1)'
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    Perfect!!! Thanks a lot. I didn't knew this. Would it be possible for you to provide some link to any bash documentation that explain the usage of quotes? – mtk Aug 12 '15 at 17:26
  • However, aside from quoting, this is also special behavior of `PS1` (and other prompt values). The value of the variable isn't simply displayed as-is; it undergoes a full round of evaluation. Note that your prompt will look different than the output of `echo "$PS1"`. – chepner Aug 12 '15 at 18:14
  • Hi chepner, please see my related question on this topic here http://stackoverflow.com/questions/32125564/how-to-display-git-branch-in-ps1-with-other-output – mtk Aug 20 '15 at 18:20
  • Hi Chepner, please can you show me a way to display additional info like datetime along with git branch. It would be really helpful. I'll try rest of the part. Sorry for pestering.. :( – mtk Aug 20 '15 at 19:38