Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't my bash prompt update?

Tags:

git

bash

ps1

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?

like image 964
greg0ire Avatar asked Mar 21 '11 15:03

greg0ire


People also ask

How do I permanently change bash prompt?

After you have experimented with text customization and colorization of your prompt, and reached a final that you want to set permanently for all your bash sessions, you need to edit your bashrc file. Save the file by pressing Ctrl+X and then by pressing Y. The changes to your bash prompt will now be permanent.

How do I get to the bash prompt in Linux?

The Bash prompt is set by the environment variable PS1 (Prompt String 1), which is used for interactive shell prompts. There is also a PS2 variable, which is used when more input is required to complete a Bash command. ... echo "Success!"

What does bash prompt mean?

PS2 bash shell variable is a secondary prompt. This prompt is displayed if the shell waits for a user input, for example you forget to insert second quotation.


2 Answers

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.

like image 93
geekosaur Avatar answered Oct 01 '22 14:10

geekosaur


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.

like image 36
Mike Seplowitz Avatar answered Oct 01 '22 15:10

Mike Seplowitz