I am trying to add Git branch name to my Bash prompt by changing the PS1 variable like this:

PS1=`echo "$PS1" | sed 's/\\\\\$ $/\$(__git_ps1)\\\\\$ /'`

And if I understand correctly, it is not recommended to update ~/.bashrc for personalizing purposes. All articles I found suggest updating either .profile or .bash_profile for such purposes. So I am looking for a perfect place to put this line into.

These are the places I've tried so far:

  • .profile: not sourced by gnome-terminal
  • .bash_profile: not sourced by gnome-terminal
  • .bash_login: not sourced by gnome-terminal
  • .bash_aliases: works, but I don't think PS1 has anything to do with aliases

I am using Ubuntu Desktop and I work in gnome-terminal or byobu, so I have to find a file which is sourced when I start bash in an X environment. Then I looked in .bashrc for other files it sources after assigning a value to PS1. There are .bash_aliases and /usr/share/bash-completion/bash_completion which I don't think are proper for this purpose.

What are other choices? And what is the best practice of customizing Bash prompt?

  • 1
    "And if I understand correctly, it is not recommended ..." - can you give is the source you are attempting to understand? – muru Mar 13 '15 at 8:37
  • And you might be better off using Bash's string substitution directly: PS1="${PS1/\$ /\$(__git_ps1)\$ }". – muru Mar 13 '15 at 8:47
  • Thanks @muru for your suggestion. That looks much cooler than what I had! – Sah Mar 18 '15 at 1:06
  • And I did a search again but unfortunately I couldn't find the articles which I thought suggested against updating .bashrc. Maybe I interpreted it wrong. – Sah Mar 18 '15 at 1:07
  • 1
    I did some experiments and this is my final version of the substitution: PS1="${PS1/%\\$ /\$(__git_ps1)\\$ }". – Sah Mar 18 '15 at 3:04

According to the following HOWTO, your .bashrc is actually the best place to tweak $PS1:

the PS1 string should be set in .bashrc. this is because non-interactive bashes go out of their way to unset PS1. the bash man page tells how the presence or absence of PS1 is a good way of knowing whether one is in an interactive vs non-interactive (ie script) bash session.


therefore it should be stressed that PS1=..blah.. should be in .bashrc and not .profile.

Source: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/setps.html


I agree with @sylvain-pineau that it should be located in .bashrc.

Im using a git-prompt shell script that I've forked on github. It works very well with git, svn and hg. It shows you which branch you are in, which files you have that is new, which files that is changed (in different colors) and the status from your last bash command.

You can download it like this:

wget https://raw.githubusercontent.com/stalet/git-prompt/master/git-prompt.sh
chmod +x git-prompt.sh

Then in .bashrc i'm using a test to see if my git-prompt script is available and then source it:

[[ -s ~/config/git-prompt.sh ]] && source ~/config/git-prompt.sh

Disclaimer, i am a commiter to the git-prompt github project.

  • Guess this looks too heavy for me since I am only using git and not svn nor hg. Plus, I don't like my console to be too colorful, I get anxious sometimes (-_-). So I think I'll accept @sylvain-pineau 's answer. But still, thanks. – Sah Mar 18 '15 at 1:11

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.