Adding Color and Customize the Bash Prompt (PS1)

Last updated: Sep 20, 2007

What is the PS1?

The PS1 is the first thing that you see when you open a terminal. In the picture below it is the green text, ‘mark@thinktank:~$’. The default bash PS1 only shows ‘[hostname]$ ‘. This is not very useful since you have to type ‘pwd’ all the time just to see what directory you are in. I prefer to my current directory in the PS1.

On the other hand the default Ubuntu/Debian prompt shows, ‘mark@thinktank:~$’ which is 100 times better than the previous example, but even it still can be improved. I prefer to have the PS1 colored because I want to have a visual way of distingusing between different user accounts on my computer. This is especially helpful for distinguishing between a regular user and root. By doing this I tend to make less mistakes at the console as root :) .

Linux Terminal

Let There be Color

Use your favorite text editor and uncomment the line in .bashrc under the line:

Comment in the above and uncomment this below for a color prompt

It may look similar to this:

PS1='${debian_chroot:+($debian_chroot)}\[33[01;32m\]\u@\h\[33[00m\]:\[33[01;34m\]\w\[33[00m\]\$ '

The code above will give you the same result picture above. Then save the file and reload your terminal (Use the Linux shortcut ctrl-d and relogin or click your terminal icon)

Make Root a Different Color

In order for root to have a different colored PS1 you need to edit root’s .bashrc config file.

Open this file with your favorite text editor. It is usually located here, ‘/root/.bashrc’.

Uncomment the same line as before to add color, and change the number 32 in the line to 31. This will change the color from green to red. See below for other colors.

PS1='${debian_chroot:+($debian_chroot)}\[33[01;31m\]\u@\h\[33[00m\]:\[33[01;34m\]\w\[33[00m\]\$ '

Now you know how to make the default bash PS1 prompt a little more useful and colorful :) . Now I will leave the rest of the post for reference material so you can customize the prompt to your own needs. Let me know what useful PS1s you come up with.

Common Bash PS1 Flags

\a an ASCII bell character (07) \d the date in “Weekday Month Date” format (e.g., “Tue May 26”) \e an ASCII escape character (033) \h the hostname up to the first `.’ \H the hostname \n newline \r carriage return \s the name of the shell, the basename of $0 (the portion following the final slash) \t the current time in 24-hour HH:MM:SS format \T the current time in 12-hour HH:MM:SS format \@ the current time in 12-hour am/pm format \u the username of the current user \v the version of bash (e.g., 2.00) \V the release of bash, version + patchlevel (e.g., 2.00.0) \w the current working directory \W the basename of the current working direc-tory ! the history number of this command # the command number of this command \$ if the effective UID is 0, a #, otherwise a $ \nnn the character corresponding to the octal number nnn *\* a backslash [ begin a sequence of non-printing characters, which could be used to embed a terminal con-trol sequence into the prompt ] end a sequence of non-printing characters

Bash Color Codes

Black 0;30

Dark Gray 1;30

Blue 0;34

Light Blue 1;34

Green 0;32

Light Green 1;32

Cyan 0;36

Light Cyan 1;36

Red 0;31

Light Red 1;31

Purple 0;35

Light Purple 1;35

Brown 0;33

Yellow 1;33

Light Gray 0;37

White 1;37

Ideas for PS1

Color change for regular user vs. root Show the current directory Show time Show date Show hostname (especially useful when “sshing” into other computers)

Need to print shipping labels on your site?

Checkout my product RocketShipIt for simple easy-to-use developer tools for UPS™ FedEx™ USPS™ and more.

Get notified on new posts or other things I'm working on

Share: