ASCII Thoughts

Configuring your Bash Prompt (PS1)

As a developer, chances are you spend a lot of time every day in your terminal. Whether you use it for building, debugging, inspecting, deploying or administering, the shell is part of your life.

Since you spend such a large amount of time using it, it makes sense to get comfortable with it. You can treat your shell as a temporary mean to an end, an impersonal hotel room of sorts, functional but never yours, or you can make it your home, a warm and cozy place, with your things, your habits and your tools.

One way to achieve that goal, to make your shell your home, is to customize it. And what better place to start than the prompt? Most Linux distributions have a slightly customized prompt, designed to work in most situations. If you're lucky, it looks like this:

user@host $ ...

If you're not, it looks like this:

$ ...

Wouldn't it be better if you could have a little more information? Maybe the time of the day, the current directory, or just some colors? Thankfully you can, and it's easy.

PS1

The variable that contains the definition of the prompt format is PS1. You can experiment with it directly in your shell:

$ PS1="% "
% PS1="! "
! ...

Apart from using a simple string, you can also use special variables such as:

\n     newline
\r     carriage return

\j     number of jobs managed by the shell

\d     date in "Weekday Month Date"
\t     current time in 24-hour HH:MM:SS format
\T     current time in 12-hour HH:MM:SS format
\@     current time in 12-hour am/pm format
\A     current time in 24-hour HH:MM format

\H     hostname
\h     hostname up to the first '.'
\u     username of the current user
\w     current working directory

There are more available, which you can find in the man (search for PROMPTING). For example, in my shell, I use the following format:

$ PS1="\u \h \t \w \\$ "
user hostname 20:46:05 ~/folder $

Colors

The shell has special escape codes for colors:

FG=COLOR_CODE
BG=COLOR_CODE

# '\033[' + FOREGROUND + '\033[' + BACKGROUND
CODE="\033[$FG\033[$BG"

The Bash Prompt HOWTO provides a script to quickly list all the colors:

Execution of script colors.sh

Together

So you can now create your own prompt, just the way you want it :) In my ~/.bashrc I have:

# We need to encapsulate with "\[ \]" to use in a prompt
BLUE1='\[\033[1;34m\]'
BLUE2='\[\033[34m\]'
GREEN1='\[\033[1;32m\]'
GREEN2='\[\033[32m\]'
CYAN1='\[\033[1;36m\]'
CYAN2='\[\033[36m\]'
RED1='\[\033[1;31m\]'
RED2='\[\033[31m\]'
PURPLE1='\[\033[1;35m\]'
PURPLE2='\[\033[35m\]'
BROWN1='\[\033[1;33m\]'
BROWN2='\[\033[33m\]'
GRIS1='\[\033[1;37m\]'
GRIS2='\[\033[37m\]'
YELLOW1='\[\033[1;33m\]'
YELLOW2='\[\033[33m\]'
WHITE='\[\033[1;37m\]'
NOCOLOUR='\[\033[0m\]'

# Don't forget to use double quotes "
PS1="$RED1\u $BLUE1\h $PURPLE1\t $GREEN2\w $BROWN2\\$ $NOCOLOUR"

Which gives:

My prompt

Going further

This was just a simple customization, but you can do much more: you can run complex commands to create your prompt, using all the tools available in your system. If you want to know more, check out:

That's it for today. Hope you enjoyed it.

Until next time, Cheers!