Chris Tankersley

Spice Up Your Shell Prompt

Posted on 2007-05-22

Ever log into a box, or install a new distro, only to find that the prompt is either just a # or it only provides very minimalistic information? Well, it's easy to change the look of the command prompt by editing the ~/.profile or other appropriate file.

Most shells look at the 'PS1' environment variable to see what the shell prompt should look like. For example, in OpenBSD the root user doesn't have PS1 set to anything, so you get just a '#' as your prompt. Not terribly useful once you start navigating through the file system, opening multiple SSH sessions to edit multiple files, and then you start to loose track of what window is what. So lets add some information to the shell prompt.

Open up the ~/.profile (or ~/.bashrc, or other appropriate file for your distro) with your favorite text editor. I use nano:

nano ~/.profile

Check to see if there is a PS1 variable already set. The line will look something like this:

PS1='[u@h]'

You can either comment the line out (which I recommend) or just alter it to your preferences.

There are a few different options that you can use to edit the look of your command prompt:

\d Date in "Weekday Month Date" format. \h Short hostname. \H Complete hostname including domain. \j Number of running jobs \l Terminal device of the shell (ex: ttyp1) \n Newline character \r Carriage return \s Name of the shell, e.g. BASH \t Current time in 24 hour HH:MM:SS format. \T Current time in 12 hour HH:MM:SS format. \u Username. \v Version of Bash \V Version of Bash with patch level \w Current working directory. \W Basename of the current working directory \$ Insert a '$' (root gets '#' instead) \ Backslash \@ Current time in 12 hour am/pm format. ! Number of commands in the history file (.bash_history). # Number of commands you have executed in your current session.

So lets set up a prompt that looks like '[username@hostname cwd]$ ' with the following:

PS1='[\u@\h \w]\$ ' export PS1

The 'export PS1' tells the shell to actually use this variable. Log out and log back in, and you will see your new prompt.

Add some color Now that we have some useful information, we can even add color (assuming your terminal supports color). To add color we get a few new commands: \e[X;Xm Start a color combination, where X;X is a combination below \e[m End color

Color

Code

Black

0;30

Blue

0;34

Green

0;32

Cyan

0;36

Red

0;31

Purple

0;35

Brown

0;33

Blue

0;34

Green

0;32

Cyan

0;36

Red

0;31

Purple

0;35

Brown

0;33

I personally like Green, so I extend my PS1 to look like this:

PS1='\e[0;32[\u@\h \w]\e[m\$ ' So, take all the above information and turn the shell prompt into something that you want to use.


Comments