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
    Commented May 3, 2016 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 Commented May 3, 2016 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.
    – Saad
    Commented May 3, 2016 at 23:32
  • You can use a function that sets PS1 using the PROMPT_COMMAND bash variable. See: PS1 example
    – cylgalad
    Commented May 4, 2016 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
    Commented May 4, 2016 at 12:29

5 Answers 5


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
    Commented May 4, 2016 at 9:09
  • update_term_cwd seems to be a deprecated Apple OS command.
    – joharr
    Commented Feb 7, 2022 at 9:20

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: '.
    – Saad
    Commented May 4, 2016 at 8:02
  • @meh_programmer I'd swapped the two branches, it should work now. Commented May 4, 2016 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
    Commented May 4, 2016 at 12:30
  • Super helpful. Thank you! Commented Aug 12, 2020 at 14:26

A one-liner:

PS1='\[\e]0;$( [ "$PS1TITLE" == "" ] || echo -n "$PS1TITLE  :  ")${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

When the variable PS1TITLE is set, its contents shows up in the window's titlebar.



Make no variable expansion.

PS1='...$( [ "$PS1TITLE" == "" ] || echo -n "$PS1TITLE  :  ")...'

Execute a test: if variable "PS1TITLE" is empty, do not display it. If the string exists and is non-empty, it is displayed with a colon.

The rest of the PS1 string is taken from a Debian system's .bashrc.

Bash executes PS1 every time prompt is displayed and updates everything accordingly.

  • Welcome to Unix & Linux and thanks for taking the time to answer this question. While your answer might be correct many readers might appreciate a short explanation how this solves the problem. You can edit your question at any time. Commented Aug 13, 2020 at 22:43
# .bashrc example edits to PS1 for git

With Abbreviated Paths:

    # -- With color:
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[01;33m\]$([[ "$(git branch --show-current 2>/dev/null)" == "" ]] || echo " ("$(git branch --show-current 2>/dev/null)")")\[\033[00m\]\$ '

    # -- Without color:
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\W$([[ "$(git branch --show-current 2>/dev/null)" == "" ]] || echo " ("$(git branch --show-current 2>/dev/null)")")\$ '

As Requested:

    # -- With color:
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;33m\]$([[ "$(git branch --show-current 2>/dev/null)" == "" ]] || echo " ("$(git branch --show-current 2>/dev/null)")")\[\033[00m\]: '

    # -- Without color:
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$([[ "$(git branch --show-current 2>/dev/null)" == "" ]] || echo " ("$(git branch --show-current 2>/dev/null)")"): '

From a KDE Plasma Bash Konsole

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .