Posted by Devin de Gruyl on May 24th, 2007
“But,” I hear you say, “it still looks really bland, doesn’t it? I mean, it’s all that boring off-white color!”
Ah, be not so quick to judge, grasshopper. Just to show you there’s hope, here’s a screenshot of my current command prompt (running in a terminal window):
Yes, Virginia, there is a color command for bash!
Here’s how I got it. From reading the first part of this article (you did read that before coming here, right?), you may be able to tell how I got the basic layout of this prompt:
export PS1=”\d \u \w \$ “
Just to review, this tells bash to display, in order, the date, your username, the current working directory (in this case ~, for my home directory), and the root/user indicator ($ for a normal user, # for root). (Also note, for the sake of consistency, I’m still using the export command to change prompts. Remember, however, that to make your changes permanent you need to enter the same PS1=”<code for your prompt>“ variable in your ~/.bashrc file.)
That’s all well and good, but how did I add those fancy colors? To explain that, I need to introduce a few new bash command codes.
If you’re familiar with basic HTML, you’re undoubtedly familiar with formatting tags such as <b> for boldface text, <i> for italics, and so on. You probably also know that a <b> tag tells a web browser where to begin displaying bold text, and a matching </b> tag tells it where to end. bash prompts work on the same principle when it comes to adding color; you use one tag to tell bash where to start displaying a color and another to tell it where to stop, or change to another color.
The tag, in this case, follows the format \e[3x;4ym, where x and y are single-digit codes for foreground (text) and background colors respectively. (Don't worry, I'll have a chart a little later on in the article so you know which numbers correspond to which digits.) For instance, the \d field in my prompt is shown as black text on a cyan background; applying this to the basic export command above would produce this:
export PS1="\[\e[30;46m\] \d \[\e[0m\] \u \w \$ “
Note a few things here. First, the use of the \[ and \] tags. These are used to surround “non-printing” command codes (such as \e), to prevent bash from getting confused. (Trust me, you don’t want to spend much time on a “confused” bash prompt!) Second, the \e[0m tag after \d, which tells bash to revert to the normal colors of your terminal.
This command, entered as printed above, will produce the following prompt:

With this pattern established, you can see how I added the rest of the color to my original screenshot. Remember: the first number in the \e[3x;4ym command determines the text color while the second is for the background. (If all you want to change is the text color, you can omit the 4y portion of the code, and bash will default to the standard background color of your terminal.) And also remember to always use the \[ and \] tags to surround your \e command, unless you want to experience a terminal window with brain damage.
Try it yourself! See if you can find a pleasing color combination for your prompt. To help you along, and as promised, here is a chart showing you what numbers to stick into your \e command code to produce colors:

(Note: Due to variances in terminal and/or monitor settings, your actual color tones may vary.)
The numbers down the left-hand side are the foreground, while the ones along the top are for the background. To get the boldfaced/brighter colors, you need to add a ;1 to the \e command (so it follows the format \e[3x;4y;1m). As you can see, there are 121 possible combinations (not counting the eight "invisible" color-on-same-color combos, such as 30;40), some more readable than others.
So, for instance, if you wanted to display "Hello, World!" as white on a blue background, you'd add this to your export command:
\[\e[37;44m\]Hello, World!
Or if you want to display a foreboding message in red text:
\[\e[31m\]I’m sorry, Dave, I’m afraid I can’t do that.
For a boldfaced message, you’d type something like:
Prompts just come \[\e[37;40;1m\]alive\[\e[0m\] with this stuff!
Remember, however, to include the \e[0m code by the end of your prompt (and surround it with the \[ and \] tags) to return to the default terminal colors, otherwise you’ll keep typing your commands in whatever color combination was the last one it encountered in your code.
With all of this in mind, the code for my personal prompt as depicted in the opening of this article is:
export PS1=”\[\e[30;46m\] \d \[\e[30;42;1m\] \u \[\e[37;44m\] \w \[\e[0m\] \$ “
As you can see, it can get a little confusing to follow once you start using multiple colors at once, but if you break it down into chunks you can see how I’ve done it. With this knowledge you can produce veritable works of art, as far as user prompts go.
And that should be enough to get you started with personalizing your bash user environment. Of course, there are far more useful things you can do at the command line level than just changing the way it looks (for instance, you could learn the fine art of shell scripting and have your computer automate various routine tasks), but I’ve found that most new users will be impressed more by eye candy than by functionality. Besides, it’s a bit of a kick being able to design your own customized user prompt… it’s like you’re a geeky Martha Stewart, or something! (Only without the Sominex voice or the naughty insider trading.)Until we meet again, remember — Don’t be afraid to experiment! Half the fun of owning a computer is in being able to fiddle with it and see what you can make it do! 8^)
Posted in code
| email this article
If you liked that, try...
- Linux Users: Pimp Your Prompt (Part 1 of 2)
- Linux Tip: ‘Embedded’ Terminal On Your Desktop
- INX: Console Computing for Non-Console Users
- Amazon MP3 Now Open To Linux Users
- HOWTO: Code a Simple Game in Python
No Comments »
No comments yet.


Podcast RSS


