Bash, the Bourne Again SHell, is the default command shell used for most Linux distributions and MacOS. One of the nice things about it (and most Unix shells, for that matter) is the flexibility you have in configuring the command prompt. When using a color-capable terminal program, I like to make the command prompt a slightly different color than the rest of the text in that window, just so it stands out when I skim backward through miles of output. There’s a couple different methods for doing that, and not all of them work well. Here’s the best method I’ve found.
The prompt string in bash is determined by the $PS1 environment variable. There are lots of different backslash-escaped codes that can be placed in that variable to make it look any way you like, but I won’t go into those here. Read this web page for more info on that. The $PS2 environment variable determines the prompt given for continued lines, such as when you type a “while” loop. The default PS1 is “u@h:w$ “, which gives the format “user@host:directory$ “. The default PS2 is simply “> “. The PS1 variable can be set in your ~/.bashrc file, if it’s not already there, using a command like this:
export PS1=”u@h:w$ “
You can run that same command from the command prompt to play with these settings interactively before you modify your .bashrc.
The general method for coloring text is to issue an escape code to the terminal program which tells it which of the handful of standard colors or typefaces should be used. By inserting this escape code into your PS1 variable, you can change the terminal’s font color when it’s drawing your prompt on the screen. I’ll show you the old school method first just for completeness, but be warned that this method gives screwy line wrapping for long command lines in bash, and should not be used.
The color-setting escape code is “e[0;3Xm”, where “X” is a 1-digit number representing the color to be used. The code to return the color to normal is “e[m”. The 0 before the semicolon means to use a normal-weighted font in the given color. Other values for that digit are:
1: boldface, or perhaps a lighter version of that color, depending on your terminal and font choice
5: blinking (gah!)
7: inverted colors
You can also add another “;4Y” before the final “m” in that escape code to control the background color. See the image below for several examples. As a simple example, the following command will keep the default command prompt format, but will color it magenta:
export PS1=”e[0;35mu@h:w$ e[m”
|A few example color schemes and the command that created each|
In the above example, you can see how much easier it is to distinguish between the output of two adjacent commands when the command prompt itself stands out like it does. So that’s what I learned recently about setting font colors in Unix shells. If you’ve got any other suggestions for useful ways to customize your desktop environments, please share them in the comments below.