How to Colorize Terminal and Shell Environment?

In Linux, the default text output color of the terminal is monochromatic. However, it can be customized to have an interactive and different interface as of default. Colorizing the terminal shell and its environment enables the user to distinguish between different terminal outputs such as file names, directories, etc. Linux provides a variety of methods to colorize terminal shells and environments.

This post will demonstrate the various ways to colorize the terminal and shell environment.

Method 1: Through ANSI Color

The ANSI color is composed of special escape sequence characters in which the background or foreground color is defined. The user can use the ANSI Code with the “echo -e” command to apply it to text, here the “e” flag is used for the escape characters. The following is the ANSI color code table:

Colors NameBackground CodeForeground Code
Red 4131
Yellow 4333
Green4232
Black4030
Cyan4636
Blue4434
Grey10090
White10797

Check the below examples to apply the ANSI code in the terminal.

Example 1: Change the Text Color

To change the foreground color of the text, pick any of the foreground color codes from the above table. Put it in the following code sequence and describe the text in the middle of the sequence as shown in the following syntax:

Syntax:

\e[<code>m <Text> \e[0m

The following echo command will change the foreground color of the text “Henry, itslinuxfoss” to the blue because of color code 34:

$ echo -e "\e[34m Henry, itslinuxfoss \e[0m"

The text color of the output has been changed.

Example 2: Change Background Color of Text

Likewise, to change the color of the text background color is obtained as follows:

$ echo -e "\e[41m Hi, Henry Here! \e[0m"

The background color has been changed to red due to color code 41.

Example 3: Adding ANSI Colors in Bash Script

Use the bash script to apply ANSI color code on multiple statements to address its purposes such as error message, success, or warning message. Let’s see how it works: 

#!/bin/bash

# set variables to use different colors
RED='\e[31m'
GREEN='\e[32m'
YELLOW='\e[33m'
ENDCOLOR="\e[0m"

# Adding color in Text
echo -e "${RED}This is the Error message${ENDCOLOR}"
echo -e "${GREEN} This is the Success Message ${ENDCOLOR}"
echo -e "${YELLOW}This is the warning${ENDCOLOR}"

In the script, the color code has been stored to the variables and these variables are used in each echo statement:

Save the above script and run the file in the terminal:

$ bash color.sh

The echo statement is properly addressing their purposes such as error, success, and warning messages.

Example 4: Adding ANSI in Prompt Shell

The user can export the ANSI color in the prompt shell using the “PS1” variable. It will change the color of the prompt string with the given color describe in the ANSI color code. The following commands are changing the prompt shell string (username and hostname) to red, green, and blue colors:

$ export PS1="\e[0;31m[\u@\h \W]\$ \e[m "
$ export PS1="\e[0;32m[\u@\h \W]\$ \e[m "
$ export PS1="\e[0;36m[\u@\h \W]\$ \e[m "

The prompt shell has been colorized in red, green, and blue color.

Method 2: Through lolcat Command 

Another way to colorize the terminal shell is using the lolcat command, this command will colorize the output of the command in rainbow color. 

Install lolcat on Linux

To install this in a Linux operating system, use the following command depending upon your Linux distribution:

$ sudo apt install lolcat      #For Ubuntu/Debian
$ sudo yum install lolcat      #CentOS/RHEL

Example:

To colorize any command output, use it with the lolcat command through a pipe:

$ ls -l | lolcat

The command output has been colorized in rainbow colors.

Method 3: Through Terminal Preferences

The user can change the color of the terminal from the terminal “Preferences”. Open the “Preferences” option as guided below:

The interface of the terminal preferences will appear, click on the profile name to change the shell environment settings of your choice. The user can change the text colors, themes, or transparency depending on the requirements. 

Conclusion

In Linux, the terminal and shell environment can be colorized using the ANSI color code, lolcat utility, or use the “preferences” option for the GUI method. To use the ANSI color code, describe the foreground or background color in the color code sequence and execute with the echo command or export them using the prompt shell variable( PS). While the lolcat utility is installed first and then use with any command through the pipe(|) utility. This write-up has illustrated the different ways to colorize the terminal shell and environment.