I'm basically trying to make my PS1 look like this when in a git repo:

$ ~/Projects/Blah (master):

Or if I'm not in a Git repo, I want it to look like this:

$ ~/Projects/Blah:

This is my current PS1:

export PS1="$ \w \$(__git_ps1): "

It works for the git repo output, but the problem is that if I'm not in a git repo, the output looks like this:

$ ~/Projects/Blah :

I don't really want that space there if it isn't a git repo, is there some way I can specify this in my PS1?

  • maybe? export PS1="$ \w \$(__git_ps1 | sed 's/^ $//'): " – Jeff Schaller May 3 '16 at 19:35
  • The space that you are referring is the one that appears before the colon?, in that case try: export PS1="$\w\$(__git_ps1):" that will remove the space, it's not related to the result of __git_ps1 – Moises Najar May 3 '16 at 22:18
  • Hmm @JeffSchaller that didn't seem to fix it I still get the space before the colon. @Moises, the the problem with that is that I get an output like this: $ ~/Projects/Blah(master):. I want a space before the parentheses if it is a git repo. – saadq May 3 '16 at 23:32
  • You can use a function that sets PS1 using the PROMPT_COMMAND bash variable. See: PS1 example – cylgalad May 4 '16 at 8:22
  • I use \[\033[01;32m\]\w\[\033[01;33m\]$(__git_ps1 " (%s)") \[\033[01;37m\]\$ and that never puts in extra spaces if I'm not in a git folder. – tgharold May 4 '16 at 12:29

What I do for this kind of thing is to change the value of PS1 when I change directories. This is trivial in zsh which executes the chpwd command; it can be done in bash by defining wrappers around cd and friends.

cd () { builtin cd "$@" && chpwd; }
pushd () { builtin pushd "$@" && chpwd; }
popd () { builtin popd "$@" && chpwd; }
chpwd () {
  if git rev-parse --show-toplevel 2>/dev/null >/dev/null; then
    PS1='$ \w $(__git_ps1): '
    PS1='$ \w: '
  • Thanks for the reply. I just tried this out, but the else part seems to never run, even when I'm in a directory that is a git repo. It seems to always be using PS1='$ \w: '. – saadq May 4 '16 at 8:02
  • @meh_programmer I'd swapped the two branches, it should work now. – Gilles 'SO- stop being evil' May 4 '16 at 9:37

I ended up using this .git-prompt.sh file. Steps to get this to work:

  1. Create a file called .git-prompt.sh in your home directory (~/.git-prompt.sh) and copy the code from the link above into it.
  2. In your .bash_profile or .bashrc file, add this line: source ~/.git-prompt.sh
  3. Change your PS1 to this: PS1='\n$ \w$(__git_ps1 " (%s)"): '
  • The key to that working is the %s in $(__git_ps1 " (%s)"). – tgharold May 4 '16 at 12:30

I use a function to set my prompt dynamically. Here is my particular function, defined in my development environment initialisation script:

function prompt_cmd
  # Tell the terminal application (using escape sequences) what the CWD is.
  # (this comes from /etc/bashrc)

  if [[ "$ORIG_PS1" == "" ]]; then
    export ORIG_PS1=$PS1

  if [[ "$CURRENT_PROJECT" == "" ]]; then
    export PS1=$ORIG_PS1
    if [[ "$PWD" == "$DEV_HOME/projects/$CURRENT_PROJECT"* ]]; then
    export PS1="\[\e[0;32m\]$CURRENT_PROJECT\[\e[m\]:$PWD_COLOR\W\[\e[m\]$ "

(it sets the path part of the prompt to red if I happen to drift out of the project I think I'm in!)

...then tell bash to use the function:

export PROMPT_COMMAND=prompt_cmd
  • Interesting to see from the .git-prompt.sh example above that you can evaluate functions in the PS1 environment variable's value - I didn't know this. Another good way of doing it! (e.g. export PS1='$(myps1fun) other stuff $ ') – pogul May 4 '16 at 9:09

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.