Customising my Bash Prompt

Bash Jul 20, 2015

Customising Bash Prompt

Update 26/07/2015: I now use variables to make the PS1 definition more readable.

Lately, I've been looking at my prompt feeling that it could be better. Even though it displayed useful information, I felt that I could make it more useful.

My new prompt is now:

RESET="\[$(tput sgr0)\]"
export PS1="$BLUE[\t] $RED\u$BOLD_WHITE@$RED\h$WHITE:\w$GREEN\$(__git_ps1 \" (%s) \")$WHITE\n\$ $RESET"

Time in bold blue:


User@host in bold red with the @ in bold white:


Git branch in bold green:

\[\e[1;32m\]\$(__git_ps1 \" (%s) \")

The actual prompt is then displayed in bold white on the next line after resetting the formatting.

\[$(tput setaf 7)\]\n\$ \[$(tput sgr0)\]"

Cheat Sheet

  • \[\e[color\] - start colouring
  • \[\e[m\]- end colouring
  • \t - current time in 24h HH:MM:SS format
  • \u - username.
  • \h - hostname
  • \w - current absolute path to working directory
  • \W - base of current working directory
  • \$ - the prompt character (eg. # for root, $ for regular users).
  • tput bold - bold text
  • tput rev - display inverse colours
  • tput sgr0 - reset everything
  • tput setaf {CODE}- foreground colour
  • tput setab {CODE}- background color