In my .bash_profile currently, I have PS1='\[\e[1;91m\][\u@\h \w]\$\[\e[0m\] '. I am trying to follow these commands to show branch name on iTerm terminal.

I changed my

PS1='\[\e[1;91m\][\u@\h \w]\[\e[0m\][\033[32m\]\$(parse_git_branch)\[\033[00m\]$'

and the method at the end of the file

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'

which shows now as:

[ava@GM12673 ~/blog][$(parse_git_branch) $

How to correctly add something to PS1?

  • 2
    Set your prompt in .bashrc, not .bash_profile. See Difference between .bashrc and .bash_profile – Gilles Aug 29 '13 at 23:05
  • Now the coloring is gone and it still does not show the branch name. – Ava Aug 29 '13 at 23:18
  • iTerm 2 opens new shells as login shells by default, so bash reads ~/.bash_profile but not ~/.bashrc. I have told iTerm 2 to open new shells as non-login shells though, and I also source ~/.bashrc from ~/.bash_profile. – Lri Aug 30 '13 at 11:14

Your quoting is broken. Change it to

PS1='\[\e[1;91m\][\u@\h \w]\[\e[0m\]\[\e[32m\]$(parse_git_branch)\[\e[00m\]$'
  • It now shows as [ava@GM12673 ~/blog][$ with no branch name. Why is an extra [ printing? – Ava Aug 29 '13 at 22:57
  • @Ava Typo, and wrong quoting. – Gilles Aug 29 '13 at 23:03
  • Now [ava@GM12673 ~/blog][32m$ – Ava Aug 29 '13 at 23:07
  • 2
    @Ava Ah, I made a typo when correcting the typo. If this doesn't work for you, you're using some non-default bash options. Post your .bash_profile and .bashrc. – Gilles Aug 29 '13 at 23:22
  • 2
    @Ava Not exactly what you asked for, but here is a nice PS1 generator: xta.github.io/HalloweenBash Gilles, thx for fixing my answer! – tlo Aug 29 '13 at 23:30

I use the parse_git_branch function myself and I've found the following works well and gives me lots of info (and a carriage return!):

parse_git_branch () {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
PS1=$PS1'\[\033[01;34m\]\w\033[00m\]:\033[01;33m\]$(parse_git_branch)\[\033[00m\]\n\$ '


enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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