Save Your Skin by Customizing Your Bash Prompt

Jack Wallen
Jan 6, 2009
Updated • Nov 28, 2012
Linux
|
7

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 [jlwallen@localhost ~]$. 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
fi

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!

Advertisement

Previous Post: «
Next Post: «

Comments

  1. Anonymous said on January 7, 2009 at 6:56 pm
    Reply

    @Wanted:

    Linux font rendering is usually a lot better in my opinion. (Compare the Eclipse IDE on Windows with the one on Linux.)

  2. Patrick said on January 7, 2009 at 4:32 pm
    Reply

    Here is mine:

    PS1=”[ [33[01;31m]u@h[33[00m] [33[01;33m]D{%a %D} @[33[00m] ] n [33[01;37m]w[33[00m] $ ”

    It displays the standard username@hostname (in red) and the day, date and time as well (in yellow). Looks sorta like this:
    [ user@ghacks Wed 01/07/09 8:30 AM ]
    ~ $

  3. Michele said on January 7, 2009 at 3:15 pm
    Reply

    Very nice!
    I’ve never understood how PS1 work… now I’m aware of

  4. jack said on January 7, 2009 at 2:49 pm
    Reply

    Wanted – which distribution are you using? do you have xft enabled? have you tried using your desktop’s control panel to tweak anti-aliasing (which is what ClearType really is)

  5. Wanted said on January 7, 2009 at 9:31 am
    Reply

    Completely off topic, but is there any hope for Linux font rendering to approach something like Windows ClearType? It’s the only thing holding me back from taking the complete plunge as I’m dreading the death of XP. Thanks!

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.