As Linux/UNIX people, we spend a lot of time working in the shell, and in many cases, this is what we have staring back at us:
bash-2.04$
If you happen to be root, you're entitled to the "prestige" version of this beautiful prompt:
bash-2.04#
These prompts are not exactly pretty. It's no wonder that several Linux distributions have upgraded their default prompts that add color and additional information to boot. However, even if you happen to have a modern distribution that comes with a nice, colorful prompt, it may not be perfect. Maybe you'd like to add or change some colors, or add (or remove) information from the prompt itself. It isn't hard to design your own colorized, tricked-out prompt from scratch.
Under bash, you can set your prompt by changing the value of the
$ export PS1="> " >
Changes take effect immediately, and can be made permanent by placing the
$ export PS1="This is my super prompt > " This is my super prompt >
While this is, um, interesting, it's not exactly useful to have a prompt that contains lots of static text. Most custom prompts contain information like the current username, working directory, or hostname. These tidbits of information can help you to navigate in your shell universe. For example, the following prompt will display your username and hostname:
$ export PS1="\u@\H > " drobbins@freebox >
This prompt is especially handy for people who log in to various machines under various differently-named accounts, since it acts as a reminder of what machine you're actually on and what privileges you currently have.
In the above example, we told bash to insert the username and hostname into the
prompt by using special backslash-escaped character sequences that bash
replaces with specific values when they appear in the
| Sequence | Description |
|---|---|
So, there you have all of bash's special backslashed escape sequences. Play around with them for a bit to get a feel for how they work. After you've done a little testing, it's time to add some color.
Adding color is quite easy; the first step is to design a prompt without color. Then, all we need to do is add special escape sequences that'll be recognized by the terminal (rather than bash) and cause it to display certain parts of the text in color. Standard Linux terminals and X terminals allow you to set the foreground (text) color and the background color, and also enable "bold" characters if so desired. We get eight colors to choose from.
Colors are selected by adding special sequences to
"\e[0m"
When we specify a zero as a numeric code, it tells the terminal to reset foreground, background, and boldness settings to their default values. You'll want to use this code at the end of your prompt, so that the text that you type in is not colorized. Now, let's take a look at the color codes. Check out this screenshot:
To use this chart, find the color you'd like to use, and find the corresponding foreground (30-37) and background (40-47) numbers. For example, if you like green on a normal black background, the numbers are 32 and 40. Then, take your prompt definition and add the appropriate color codes. This:
$ export PS1="\w> "
becomes:
$ export PS1="\e[32;40m\w> "
So far, so good, but it's not perfect yet. After bash prints the working
directory, we need to set the color back to normal with an
$ export PS1="\e[32;40m\w> \e[0m"
This definition will give you a nice, green prompt, but we still need to add a
few finishing touches. We don't need to include the background color setting of
40, since that sets the background to black which is the default color anyway.
Also, the green color is quite dim; we can fix this by adding a
$ export PS1="\[\e[32;1m\]\w> \[\e[0m\]"
Don't be afraid to use several colors in the same prompt, like so:
$ export PS1="\[\e[36;1m\]\u@\[\e[32;1m\]\H> \[\e[0m\]"
I've shown you how to add information and color to your prompt, but you can do
even more. It's possible to add special codes to your prompt that will cause
the title bar of your X terminal (such as rxvt or aterm) to be dynamically
updated. All you need to do is add the following sequence to your
"\e]2;titlebar\a"
Simply replace the substring
$ export PS1="\[\e]2;\u@\H \w\a\e[32;1m\]>\[\e[0m\] "
This is the particular prompt that I'm using in the color chart screenshot,
above. I love this prompt, because it puts all the information in the title bar
rather than in the terminal where it limits how much can fit on a line. By the
way, make sure you surround your titlebar sequence with
if [ "$TERM" = "linux" ]
then
# We're on the system console or maybe telnetting in
export PS1="\[\e[32;1m\]\u@\H > \[\e[0m\]"
else
# We're not on the console, assume an xterm
export PS1="\[\e]2;\u@\H \w\a\e[32;1m\]>\[\e[0m\] "
fi
This bash conditional statement will dynamically set your prompt based on your
current terminal settings. For consistency, you'll want to configure your
source ~/.bashrc
This way, you'll get the same prompt setting whether you start a login or non-login shell.
Well, there you have it. Now, have some fun and whip up some nifty colorized prompts!
Daniel Robbins lives in Albuquerque, New Mexico. He was the President/CEO of
Gentoo Technologies Inc., the Chief Architect of the Gentoo Project and is a
contributing author of several books published by MacMillan: Caldera OpenLinux
Unleashed, SuSE Linux Unleashed, and Samba Unleashed. Daniel has been involved
with computers in some fashion since the second grade when he was first exposed
to the Logo programming language and a potentially lethal dose of Pac Man. This
probably explains why he has since served as a Lead Graphic Artist at SONY
Electronic Publishing/Psygnosis. Daniel enjoys spending time with his wife Mary
and his new baby daughter, Hadassah. You can contact Daniel at