Enterprise Software

You're not just a number: Customizing your bash prompt

Customizing your bash prompt is an art that has been taken to many heights. What has come out of this expression is that, as an administrator, you can use prompt customization to your advantage. Read on and learn how.

[mailto:username@hostnameusername@hostname directory]$ mv pretty_boring unique
URHERE: echo 'isn't that better?'

Let me explain the above. This little expression highlights two very simple ways the bash prompt (that ubiquitous place where Linux commands are entered) can be displayed. The former is the default, which consists of an opening bracket, a username followed by the hostname (of the working machine), followed by the working directory, followed by a closing bracket, and the dollar sign (unless you are root, and then the dollar sign is replaced by a pound symbol). Or better yet, Figure A and Figure B show a more pronounced difference:

Figure A
The default bash prompt

Figure B
A unique bash prompt

“How is this magic pulled off?” you ask. “Ever so simply,” I reply.

Safety first
I will begin this little exercise by first saving my default bash prompt. By doing this, I ensure that I will have a default to fall back on (should I render my prompt unreadable).

To save my current, working prompt, at the prompt I type:

Whatever state you are in, you should be able to restore your working prompt by typing:

To continue with my safety-first theme, I am going to play with the prompt, and then, once I have it exactly as I want it, I'll show you how to make the changes permanent (until I become bored and want to change it again).

Temporary changes
To make changes permanent, you will enter your configurations within an rc (resource control) file. Your first steps, however, will be to make temporary changes, and you'll do this from the command line (as you did when saving your original prompt state).

Let’s stick with the example from Figure B (above) for the time being. Obviously, the first example:
[username@hostname directory]$

is the default, so no configuration is necessary. The second example, however, needs some configuration. Before you jump into making changes, let's take apart the default example.

To make any changes to your currently running prompt, use the PS1= command (followed by your user-defined variables. The user-defined variables are the key here. The most popular variables include the following:
  • \a—ASCII bell character
  • \d—The date (in weekday, month, date format)
  • \h—Hostname up to the first ‘.’
  • \H—Full hostname
  • \n—New line
  • \r—Carriage return
  • \s—Name of the shell (i.e., bash)
  • \t—Current time (24 hour format HH:MM:SS)
  • \T—Current time (12 hour format HH:MM:SS)
  • \—Current time (12 hour A.M./P.M. format)
  • \u—Username
  • \w—The current working directory—full path
  • \W—The basename of the current working directory
  • \!—The history number of this command
  • \#—The command number of this command
  • \$—If the UID is 0, then a # will be displayed, other wise $
  • \\—A backslash

Obviously, you have a lot of choices for your bash prompt. Let's take a look at what created your default prompt, [username@hostname directory]$. To set up the default prompt, you will need to use the following arguments:
  • \u
  • \h
  • \W
  • \$

With some added formatting—including the brackets and the at symbol (@)—that's all you need. So, for this default prompt, run the command:
PS1="[\u@\h \W]\$ "

What you see above is the PS1= command (mentioned earlier), followed by the configuration of your prompt. The configuration options will be enclosed in double quotation marks, and you'll notice a final space before the last quote. Within the quotes, you will see the familiar options used to create your default prompt (mentioned earlier), your braces, a stray at symbol (hey, it's not actually stray per se), and your space (used for clarity).

Huh? Okay...Let's break this down for clarity's sake. You know that everything you want in your prompt will be surrounded by the quotes, so you don't need to explain them any further. The first character you see, [, is a standard that has been accepted within the Linux/UNIX community. Your braces will hold the relevant information for your prompt. This, of course, can be any ASCII character you wish, but we'll stick with the braces. The next section is actually your first argument, \u, which adds your username to the prompt. The next character, the at symbol, is a bit different. Notice that the at symbol is not preceded by a \ character. This tells bash that it will simply be a character displayed within the prompt and not a return character (return characters meaning a character, or character sets, with special meaning), so the at symbol will be displayed directly after your username.

So far, your command looks like:

and your prompt looks like:

and is incomplete. You follow up what you have with the \h argument that adds your hostname (only up to the first '.') to the mix. Now, your command looks like:

Check out your progress. You’ll see:

To make your prompt more useful, add your current working directory (but not the entire path) with the \W argument. For readability’s sake, you’ll want to add a space before the directory. So, add a space between the \h and the \W. At this point, you have a command like:
PS1="[\u@\h \W

and a prompt looking similar to:
[username@hostname directory

You close up your prompt with an ending bracket (]), and then add the \$ so that you have something to tell you if you are a user or root. Now, your command should look like:
PS1="[\u@\h \W]\$"

and your prompt should look like this:
[username@hostname directory]$

Notice the space between the \$ and your final quote—this is simply for readability’s sake. With your final quote, you are done!

Make it personal now
Now, you are going to reconfigure your prompt to be just a bit more personal. As you can see from Figure B, you are going to create a yellow prompt that says URHERE:. To do this, you will be using some different characters (and combinations therein) not typically used in the standard prompt.

The command to get this new, unique prompt looks like this:
PS1='\[\033[1;33m\]URHERE: \[\033[0m\] '

What does this mean? Let's tear it apart and find out. The first sequence, '\[\033[1;33m\] does the following:
  • '—This begins the string.
  • \[—This begins a string of nonprinting characters.
  • \—This is the escape character preserving the literal value of the next character(s).
  • 033—This is ASCII code for the escape key.
  • [—This is used to separate the sequences.
  • 1;33m—This defines the color yellow.
  • \]—This ends the string of nonprinting characters.

The next section, URHERE: prints your message, URHERE, followed by a colon (:) and a space (for clarity). The final section, \[\033[0m\] ', ends your prompt with the following:
  • \]—This begins a string of nonprinting characters.
  • \—This is the escape character preserving the literal value of the next character(s).
  • 033—This is ASCII code for the Escape key.
  • [—This is used to separate the sequences.
  • 0m—This ends the color definition.
  • \]—This ends the string of nonprinting characters.

There you have it. Of course, you can customize this even further by changing colors or printed messages. To change the colors, you will use the following characters:
  • 1;37m—White
  • 37m—Light gray
  • 1;30m—Gray
  • 30m—Black
  • 31m—Red
  • 1;31m—Light red
  • 32m—Green
  • 1;32m—Light green
  • 33m—Brown
  • 1;33m—Yellow
  • 34m—Blue
  • 1;34m—Light blue
  • 35m—Purple
  • 1;35m—Pink
  • 36m—Cyan
  • 1;36m—Light cyan

One area in which I find color useful is in defining various machines. When logged into more than one machine, I like to be able to tell which machine I'm working with (before I run the one command on the wrong machine that could bring down the entire network). Because of this, I will choose a color scheme such as:
  • Blue for mail server
  • Yellow for FTP server
  • Red for firewall
  • Green for workstation

That way, I always know the machine to which I'm about to send a command.

Making your changes permanent
Making these changes permanent is rather simple. Located in your user directory is a file called .bashrc, which houses these types of configurations. Open up this file in your favorite editor, and you'll see something akin to:
# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
  . /etc/bashrc

This is the default file, and since there are no configurations for the bash prompt, it will look to the global configuration file, /etc/bashrc, for its definitions.

For the default prompt, you do not change your .bashrc file. For your user-configured prompt, you will want to enter the same line as you did earlier. Under the User specific aliases and functions section add:
PS1='\[\033[1;33m\](\W): \[\033[0m\] '

Once you've added the above line, save the file and open up a new terminal emulator.
Do not close your original terminal window until you know for sure that your .bashrc file is okay. This way, if it does not work, you will still have an open terminal in which to fix the problem.
Obviously I've only touched on the power of the bash prompt. I hope, however, this has given you a springboard to launch you, and your prompts, to limitless horizons.

Enjoy playing around with various prompts. Be creative and e-mail me if you come up with a really clever prompt idea.
The authors and editors have taken care in preparation of the content contained herein but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for any damages. Always have a verified backup before making any changes.


Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks