Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing git branch in shell prompt?

I am trying to get my shell prompt to display the current git branch name.

I have read a few tutorials and blog posts etc. and as far as I understand I'm doing everything correctly but it doesn't seem to be working.

I would like the prompt to look like this:

dannys-macbook:hillcrest-store [master]$

but currently it looks like this:

dannys-macbook:hillcrest-store danny$ 

I have added the following to ~/.bash_profile:

PATH=$PATH:/usr/local/bin; export PATH

COLOR1="\[\e[1;32m\]"
COLOR2='\[\e[1;1m\]'
COLOR3='\[\e[m\]'
GIT_STATUS=$(__git_ps1 " %s")
PROMPT_CHAR="$"

PROMPT="${COLOR1}\u@\h${COLOR3} \w${COLOR2}${GIT_STATUS} ${COLOR2}${PROMPT_CHAR$
PS1="$PROMPT"
export PS1

I'm not sure what I'm doing wrong, maybe I should be 'resetting' the prompt somehow?

like image 586
dannymcc Avatar asked Dec 12 '22 09:12

dannymcc


1 Answers

Simpler solution: quote the GIT_STATUS so it doesn't get evaluated on bash startup, but instead gets evaluated when bash is displaying the prompt:

COLOR1='\[\e[1;32m\]'
COLOR2='\[\e[1;1m\]'
COLOR3='\[\e[m\]'
GIT_STATUS='$(__git_ps1 " %s")'
PROMPT_CHAR='\$'
PS1="${COLOR1}\u@\h${COLOR3} \w${COLOR2}${GIT_STATUS} ${COLOR2}${PROMPT_CHAR}"

Also note that exporting PS1 is not a good idea.

like image 165
Marius Gedminas Avatar answered Dec 23 '22 18:12

Marius Gedminas