jump to navigation

Customizing Your Bash Prompt on Linux February 4, 2011

Posted by Tournas Dimitrios in Linux.

If you do much work at the Linux command line then you know the bash prompt can offer you a lot of information. But by default the prompt itself isn’t too useful.  What you might not know is that you can customize the bash prompt in many ways. From configuring colors to the information bash reports, there are so many variations on the bash prompt you could play for days.

But there is one configuration you can do that is more helpful than any other. Have you ever accidentally issued a standard user command as the root user only to find yourself really regretting that command a millisecond later? It’s a common new Linux user mistake, but one that can be prevented. You’ll learn how to help yourself out here.

Before we get into the actual configuration (and how you can help yourself), let’s take a look at some basics. From your command line issue the following command echo $PS1. What you should see is the string that comprises your current default bash prompt. When I issue this command I see [\u@\h \W]\$ and my prompt looks like [root@Homeserver ~]#.

Let me explain what the components of the string are
[ When used alone this is simply a printed character.
\u This prints out the current username.
@ When used alone this is simply a printed character.
\h This prints out the hostname of the machine up to the first dot.
\W This prints out the basename of the current working directory (with the users home directory represented by the “~” character.
] When used alone this is simply a printed character.
\$ If root user this prints a “#” character, otherwise it prints a “$” character.

So let’s say you want to be clever and have a prompt that looks like URHERE (~):
To create this issue the command: PS1=”URHERE (\W): “
What the above command does is temporarily set your bash prompt. This prompt will last until you close out your terminal window. When you open a new terminal your default prompt will return.
To make this permanent you will need to open up your .bashrc file and add the line you entered as a command. The default .bashrc file might look like:

# .bashrc
# User specific aliases and functions
? () { echo “$*” | bc -l; }
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc

If you want to make this permanent add the line PS1=”URHERE (\W): “ right under the # .bashrc line. Save that file and open up a new prompt. Voila!

Adding Color:

Okay, let’s take it to 11. One trick I like to employ is configuring the root prompt to be a different color than the standard user prompt. This way, if I see a red prompt, I instantly know I am dealing with the root user. Here is the trick with adding color. First you have to use a special string to indicate the beginning of a color.
That string is: \e[
Now to end a color you use the special string:  \e[m
For the color red you would use the string:  1;31m
So if you want the string URHERE to show up in red and the directory to remain the default white you would enter into the root user’s .bashrc file:
PS1=”\e[1;31mURHERE\e[m (\W): ”

Here are the various colors you can use.

  • 0;30 – Black
  • 0;31 – Red
  • 0;32 – Green
  • 0;33 – Brown
  • 0;34 – Blue
  • 0;35 – Purple
  • 0;36 – Cyan

The 0 equals the dark variation of the color and a 1 equals the lighter variation of the color.

Final Thoughts :
You can really get creative with the bash prompt. With the basics you have learned here you can make your prompt both unique and helpful. If you come up with something really incredible post it here for all to enjoy!


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s