Bash prompt customization

Bash when is executed as interactive, uses environment variables to define how the shell prompt looks in different scenarios. One of those variables is PS1, this variable is used to define the prompt displayed when the shell is ready to read a command(this is the one we will normally refer to when we say we would like to customize our prompt).

There are more variables like: PS2 is displayed when the shell needs more input to complete a command, or PS0 that is displayed after it reads a command but before it is executed. But we will only focus on PS1 just now, there is more information on the man page of bash (run the command man bash and search for PS1).

The place holders are:

              \a     an ASCII bell character (07)
              \d     the  date  in "Weekday Month Date" format (e.g., "Tue May
                     26")
              \D{format}
                     the format is passed to strftime(3)  and  the  result  is
                     inserted  into the prompt string; an empty format results
                     in a locale-specific time representation.  The braces are
                     required
              \e     an ASCII escape character (033)
              \h     the hostname up to the first `.'
              \H     the hostname
              \j     the number of jobs currently managed by the shell
              \l     the basename of the shell's terminal device name
              \n     newline
              \r     carriage return
              \s     the  name  of  the shell, the basename of $0 (the portion
                     following the final slash)
              \t     the current time in 24-hour HH:MM:SS format
              \T     the current time in 12-hour HH:MM:SS format
              \@     the current time in 12-hour am/pm format
              \A     the current time in 24-hour HH:MM format
              \u     the username of the current user
              \v     the version of bash (e.g., 2.00)
              \V     the release of bash, version + patch level (e.g., 2.00.0)
              \w     the  current  working  directory,  with $HOME abbreviated
                     with a tilde (uses the value of the PROMPT_DIRTRIM  vari-
                     able)
              \W     the basename of the current working directory, with $HOME
                     abbreviated with a tilde
              \!     the history number of this command
              \#     the command number of this command
              \$     if the effective UID is 0, a #, otherwise a $
              \nnn   the character corresponding to the octal number nnn
              \\     a backslash
              \[     begin a sequence of non-printing characters, which  could
                     be  used  to  embed  a terminal control sequence into the
                     prompt
              \]     end a sequence of non-printing characters

For example, if we wish to display the current user and the base name of the current directory we would set PS1 to:

1
PS1="\u\W > "

If we try that out it looks very plain we could add some separator between the user and the working directory and we could also display some colour.

To display colour on the shell, we use the ANSI scape codes, for example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12

blu=$'\[\e[1;34m\]'
cyn=$'\[\e[1;36m\]'
end=$'\[\e[0m\]'
grn=$'\[\e[1;32m\]'
mag=$'\[\e[1;35m\]'
p_end=$'\e[0m'
p_gry=$'\e[38;5;59m'
p_red=$'\e[1;31m'
prp=$'\[\e[0;33m]\]'
red=$'\[\e[1;31m\]'
yel=$'\[\e[1;33m\]'

With this colour definition, we can then change the color of some parts of the prompt individually:

1
PS1="${blu}\u${grn}@${yel}\h${mag}:${PS1}${end}\W${red}> ${end}"

Note that if we don’t use the $end sequence, all text after will be modified, so to enclose the colour change use the $end color sequence that turns the text back to normal.

Another improvement to the prompt is showing the git status of the current working directory. For this, we will use the PROMPT_COMMAND environment variable, the content of this variable is executed right before the prompt is displayed.

We are going to use two very simple functions, one to check if the current directory is “dirty” on any way, meaning there are any unstaged and/or modifications for git, and we are going to show the branch we are currently on. Here are the two functions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
parse_git_dirty() {
  dirty_git=$(git status --porcelain 2> /dev/null)
  if [[ -n $dirty_git ]]; then
    echo "*"
  fi
}

parse_git_branch() {
  echo $(git rev-parse --abbrev-ref HEAD 2> /dev/null)
}

Then we can call them on our PROMPT_COMMAND variable:

1
PROMPT_COMMAND='printf "${p_gry}%$(($COLUMNS + 17))s${p_end}\r" "$(parse_git_branch) ${p_red}$(parse_git_dirty)${p_gry} ($(date +%m/%d\ %H:%M:%S))"'

We are using $COLUMNS to obtain the number of columns on the screen so we can display the information on the right-hand side of the screen.

And that’s it, with this as a base we can customize our prompt to give us the information we need and make our development environment more efficient.

PS. my dot files are here


** If you want to check what else I'm currently doing, be sure to follow me on twitter @rderik or subscribe to the newsletter. If you want to send me a direct message, you can send it to derik@rderik.com.
$