Monday 30 June 2008

Customizing BASH command prompt

The prompt text can hold more information than just username and hostname, such as command numbers, time, date, shell version etc.
The text for the prompt is held in environment variable PS1 (Prompt String 1), and all that needs to be done is to change that variable.
Default prompt on most Linux systems consists of username, hostname and current working directory, something like this:
username@hostname:~$ 

The prompt is fully customizable and the PS1 variable can be changed to users needs. We can have either extra short prompt with just one symbol, or a very long one showing all sorts of information in all sorts of colors. Simple example would be prompt consisting of simple series of characters, for example a 'my prompt: ' character. So we assign a new value to PS1:
tvrtko@tvrtko-laptop:~$ PS1='my prompt: '
my prompt:
my prompt:

Prompt string can have a wide range of characters, escaped control sequence characters and color codes.
Here are bash escaped control sequence characters:

* \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 and than passed to the prompt
* \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
* \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
* \] : end a sequence of non-printing characters

Now lets set a prompt displaying current time in 12-hour format, username, the command number and the '$' sign at the end:
my prompt: PS1='\T-\u-\# $ '
10:22:28-tvrtko-4 $
10:22:32-tvrtko-4 $ ls -a
...
10:22:38-tvrtko-5 $

How to add colors to a prompt?

This might look a little complicated at start but its really not a big deal. To add colors to the shell prompt use the following export command syntax:
'\e[x;ym $PS1 \e[0m'
Where:
* \e[ - Start color scheme
* x;y - Color pair to use (x - foreground;y - background)
* $PS1 - your shell prompt
* \e[0m - Stop color scheme

foreground colors:
* 30 black
* 31 red
* 32 green
* 33 brown
* 34 blue
* 35 purple
* 36 light
* 37 gray

background colors:
* 40 black
* 41 red
* 42 green
* 43 brown
* 44 blue
* 45 purple
* 46 light blue
* 47 white

Now lets set the new color prompt with light blue background displaying 24hr - time in black, username in brown and the '$' sign in black again:



Notice that the \e[0m end sequence brings back the default color. To set text after the prompt in brown foreground and black background we would end sequence with \e[33;40m.
To make prompt string effective every time bash starts put your PS1='...' line in .bashrc file.

Well, I hope this guide was clear to understand. Have fun.

1 comment:

Eric Njogu said...

Thanks for the nice and helpful article.

The black that I specify in the prompt stands out from the pure black of the bash shell.