Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
1  
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. –  ؘؘؘؘ Aug 30 '13 at 11:14

2 Answers 2

up vote 0 down vote accepted

Your quoting is broken. Change it to

PS1='\[\e[1;91m\][\u@\h \w]\[\e[0m\]\[\e[32m\]$(parse_git_branch)\[\e[00m\]$'
share|improve this answer
    
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='\033[01;31m\]\t\033[00m\]:'
PS1=$PS1'\[\033[01;32m\]\u@\h\[\033[00m\]:'
PS1=$PS1'\[\033[01;34m\]\w\033[00m\]:\033[01;33m\]$(parse_git_branch)\[\033[00m\]\n\$ '
PS2='\[\033[01;36m\]>'

for

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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