Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Many people are afraid to start adding colors to their shells because, at first, it looks like some kind of weird arcane art. But that just isn’t true, and all you need in order to start adding colors to your prompt and commands is just grasp a few simple concepts.
Before we dive into the Linux terminal color system we’ll explain some useful definitions and concepts that will help you to understand how colors work in Linux:
Most terminal coloring, including colorizing your prompt, in Linux and macOS is done through the ANSI color coding syntax. The full syntax archetype is as follows:
\e[attr;bg;fgm YOUR COLORIZED TEXT {\e}[m
Where:
Important note: that’s the full color statement, you don’t have to fill all the fields when colorizing your text. If you don’t want effects, ignore attr, if you want a transparent background, ignore bg, etc.
echo -e "\e[00;31mTHIS IS RED\e[00m" |
The previous echo command (we used the -e flag to tell echo to enable ‘interpret backslash escapes’) will output the following:
Now let’s make a small modification to the previous command and change the attr to 01 (bold).
echo -e "\e[00;31mTHIS IS RED\e[00m" |
As we can see it emboldened our text. Now let’s add a background color and change the text color:
echo -e "\e[1;41;33m YELLOW TEXT WITH A RED BACKGROUND \e[m" |
Now that you know how to add color to your outputs in Linux, you’ll need the color codes (otherwise you’ll have to keep guessing values until you find the color you want). You can either go to an Internet page listing the codes, or, even better, install a color displaying tool (this is more useful because it’ll show you the color available in your system).
The best color displaying tool is colortext. Once you install it (using apt, pacman, or your system’s package manager), you’ll have multiple available commands to display color codes at your disposal. These are:
Here’s the output of colortest-16b
An even easier way to input color codes is just using tput. The syntax couldn’t be any easier.
You can use the following table as a guide:
Tput text modes
Since tput is a command, if we want to use it inside environment variables or in other commands we’ll have to use a mechanism called command substitution. In a few words, command substitution is a way to execute a command inside another command, and we achieve that by enclosing the subcommand inside: $( )
For example, if we want to assign a color code to a variable using tput, we’ll have to use the following syntax:
Then, when we want to use that color, we’ll just invoke the variable using the following syntax ${VARNAME}. Or we can alternatively just use tput directly (that’s what we’ll do next).
In this example we’ll colorize a bash prompt (if you use another shell you may need to edit a different file). In short, bash’s configuration parameters are stored in the .bashrc file. In that file we’ll find the lines that configure the prompt structure. The prompt is usually stored in the PS1 environment variable. If you just want to experiment different prompt coloring configurations without making any permanent changes you can just use the export command (we’ll use this approach in the examples).
Important: Each sequence of coloring and styling must be enclosed between \[ \]
export PS1= "\[$(tput setb 6)$(tput setaf 5)\]\u@\h:\w $ \[$(tput sgr0)\]" |
As you can see we use tput sgr0 at the end to stop all coloring, otherwise our entire command output will be colorized along with the prompt. The previous example uses only a color statement (plus the turn off statement at the end) which basically will turn our entire prompt purple.
Now let’s make the @ and the hostname bold and also let’s change the hostname’s color.
export PS1= "\[$(tput setb 6)$(tput setaf 5)\]\u\[$(tput bold)\]@\[$(tput setaf 1)\h:\w $ \[$(tput sgr0)\]" |
If you decide to make your changes permanent, which means you’ll rewrite the contents of your PS1 environment variable inside .bashrc you can just use variables to store the color codes and make everything more legible. For example:
PUR= "[$(tput setaf 5)]" BAC= "[$(tput setb 6)]" RED= "[$(tput setaf 1)]" BOLD= "[$(tput bold)]" END= "[$(tput sgr0)]" PS1= "${BAC}${PUR}\u${BOLD}@${RED}\h:\w $ ${END}" |
As you can see using variables makes the prompt configuration much more legible.
The mechanism is similar. Same as with tput you’ll also have to enclose the different color statements in ANSI between \[ \]
PS1= "\[\e[31m\]\u\[\e[m\]\[\e[31;30m\]@\[\e[m\]\[\e[34m\]\h\[\e[m\]:" |
Same as before, we use an empty color statement at the end \e[m in order to stop the coloring because otherwise your whole output will be colorized every time you run a command.
Another important aspect of shell coloring is the color of your directories and filetypes, specially when you run any directory and file listing commands, like for example ls, which use the dircolors GNU tool. These commands usually take the coloring information from a file called .dircolors which maps different color codes to different types of files (and whose contents are stored in the LS_COLORS environment variable once the shell is initialized).
You can either create your own filetype color mapping file, or use any of the many templates available on the Internet. One of our favorite tools is the LS_COLORS project. A GitHub resource which maps around 300+ different filetype and extension color mappings.