Abstract colors.

How to add colors to your Linux shell and prompt

In this guide you'll learn how to use the Linux and macOS syntax to add color to your shell and prompt: colorizing the bash sell.

Coloring your Linux and macOs shell

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.

Some useful 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:

  • Extended color map: the map of color codes defined in the ECMA-48 document. These are usually the rich colors used in terminals that support displaying 256 colors (nowadays that’s almost universal).
    • You can check anyway if your shell supports 256 colors by running this command: tput colors
  • \e is the shell’s escaped char, we’ll use it to introduce color statements.

The ANSI color coding

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:

  • \e[ is the ANSI escape-sequence.
  • attr is the attribute.
    • 00 (no attribute), 01 (bold), 04 (underline), 05 (blink)
  • bg is the background color.
  • fg is the foreground color.
  • m is the closing statement
  • “YOUR COLORIZED TEXT” is the text to be colorized.
  • \e[m that’s basically an empty color statement, and it resets the coloring (otherwise all the text you write after your first statement will be colorized). In short, we use \e[m when we want to stop colorizing text.
    • Some people also write it as \e[00m, but you don’t need to include the 00. In the examples we’ll use both syntaxes.

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.

Examples

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"
Coloring commands in the Linux shell.

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"

Color codes

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:

  • colortest-16 will show you the available colors
  • colortest-16b will show you the color codes.
  • colortest-256 will show you colorcubes and let you know if you have 256 colors available.

Here’s the output of colortest-16b

Colorizing with tput

An even easier way to input color codes is just using tput. The syntax couldn’t be any easier.

  • tput setab <code> – set a background color using ANSI escape
  • tput setb <code> – set a background color
  • tput setaf <code>– set a foreground color using ANSI escape
  • tput setf <code> – Set a foreground color

You can use the following table as a guide:

Tput text modes

  • tput bold – set bold mode
  • tput dim – half-bright mode
  • tput smul – underline mode
    • tput rmul – exit underline mode
  • tput smso – standout mode (bold on rxvt)
    • tput rmso – Exit standout mode
  • tput sgr0 – Turn off all attributes
    • Important! This is how we turn coloring off.
    • sgr0 is the tput equivalent to the empty color statement we use in ANSI to stop or reset the coloring.

Using tput

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).

Colorizing the prompt

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 \[ \]

exportPS1="\[$(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.

Colorizing the prompt with tput.

Now let’s make the @ and the hostname bold and also let’s change the hostname’s color.

exportPS1="\[$(tput setb 6)$(tput setaf 5)\]\u\[$(tput bold)\]@\[$(tput setaf 1)\h:\w $ \[$(tput sgr0)\]"

Using variables

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.

Colorizing the prompt using ANSI colors

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.

Colorizing directories

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.

Leave a Reply

Your email address will not be published. Required fields are marked *