How to Change the Output Color of echo in Linux

Changing the colors of the commands and customizing the Linux, is one of the reasons that users are switching to it. Linux provides many options to customize the Linux, whether related to desktop environments or the colors of commands. 

The echo command of Linux displays or prints the messages to the screen. The methods of changing the echo command’s output color have been covered in this article. 

Why does Linux we Change echo Command’s Output Color?

The output of the echo command can be colored in variety of colors because of the below-mentioned reasons:

  • It can be used to differentiate the information of the echo command
  • It can be used to highlight the warning messages
  • It can enhance the readability of the messages
  • It can make an appealing impact on the users

What are the ANSI Escape Codes in Linux?

ANSI Escape Codes is the combination of different characters used for the formatting and customization of the text in the terminal. The following table explains the ANSI codes for changing the colors of the text of echo command’s in Linux.

ColorsANSI CodeColorsANSI Code
Black0;30Dark Gray1;30
Red0;31Light Red1;31
Green0;32Light Green1;32
Orange0;33Yellow1;33
Blue0;34Light Blue1;34
Purple 0;35Light Purple1;35
Cyan0;36Light Cyan1;36
Light Gray0;37White1;37

Additionally, the below-mentioned ANSI Escape Codes are used to change the color of the background:

ColorsANSI CodeColorsANSI Code
Black0;40Dark Gray1;40
Red0;41Light Red1;41
Green0;42Light Green1;42
Orange0;43Yellow1;43
Blue0;44Light Blue1;44
Purple 0;45Light Purple1;45
Cyan0;46Light Cyan1;46
Light Gray0;47White1;47

How to Use the ANSI Escape Codes in Linux?

To use the ANSI Escape Codes in Linux, follow the instructions:

  • First, define the ANSI Escape Code either by using the “\e” or “\033
  • Then use the [” symbol
  • After this, use the number of the color explained in the above table
  • When the color is defined, instruct the command to set the color against the mentioned “ANSI Escape Code” by using the letter “m
  • Write the text on which the color is supposed to be applied
  • Finally, close the command by using the “\e[

By following these instructions, users can set different colors to the text. A general syntax according to the above instructions is:

"\e[ANSI_of_colormMESSAGE\e["

This can be used either in the bash scripting or in the terminal.

How to Check the Compatibility of Terminal with the ANSI Escape Codes?

There are some Linux distributions that might not support the ANSI Escape Codes. To check the compatibility of your Linux terminal with the ANSI Escape Codes, run the command:

$ tput colors

The output displays some value and if it is greater than zero, it means the terminal supports the ANSI Escape Codes.

How to Change the echo Command’s Output Color in Linux?

To change the color of the output of the “echo” commands, follow the instructions explained in the usage section of ANSI Escape Codes. Additionally include the “e” option when using the escape sequence with the echo command. For example, display the welcome message with the green color using the echo command:

$ echo -e "\e[32mWelcome to itsLinuxFoss\e[0m"

In the above command, the “\e[0” is used to terminate and reset the text to the default color. 

How to Change the Color of echo Command’s Output Using the Bash Scripting in Linux?

Using bash scripting is another method of changing the color of the output of the echo command in Linux. In the bash script, ANSI Escape Codes of different colors are stored in variables and then used in the echo command. 

To understand it, create and open a new file with the nano text editor:

$ nano myFile.sh

Now write the simple bash script by using the ANSI Escape Codes in the variables:

#!/bin/bash

light_gray='\033[0;37m'

cyan='\033[0;36m'

purple='\033[0;35m'

echo -e "${light_gray}Welcome to itsLinuxFoss"

echo -e "${cyan}Welcome to itsLinuxFoss"

echo -e "${purple}Welcome to itsLinuxFoss"

Close the file by saving it with the shortcut key of CTRL+S and then run the bash script:

$ bash myFile.sh

Colorful messages are displayed on the screen in Linux. 

How to Change the echo Command’s Background Color in Linux?

To change the background color of the echo command in Linux, simply use the ANSI Escape Codes explained in the second table. Use the following command, for instance, to change the echo command’s output to “Blue”:

$ echo -e "\e[44mWelcome to itsLinuxFoss\e[0m"

How to Change the Background Color of echo Output Bash – Linux?

Simply change the ANSI code to alter the background color of the output of echo command using the bash scripting. For example, in the myFile.sh, change the bash script with the below-mentioned:

#!/bin/bash

light_gray='\033[0;47m'

cyan='\033[0;46m'

yellow='\033[1;43m'

echo -e "${light_gray}Welcome to itsLinuxFoss"

echo -e "${cyan}Welcome to itsLinuxFoss"

echo -e "${yellow}Welcome to itsLinuxFoss\e[0m"

Run the bash script with the below-mentioned command after saving the nano text editor’s file:

$ bash myFile.sh

The background colors are successfully set in the output of the bash script. 

How to Change the echo Command’s Output Color including Text and Background in Linux?

To change both the color of the background and the text of the echo command’s output, define both ANSI Escape Codes. For example, set the color of the echo command’s text to green and the background color to light grey with the command:

$ echo -e "\e[32m\e[47mWelcome to itsLinuxFoss\e[0m"

How to Use the tput Command to Change the Output Color of the echo Command in Linux?

Another approach to changing the output color of the echo command is by using the tput command. Color codes for the tput command are as

ColorNumber
Black0
Red 1
Green2
Yellow3
Blue4
Magenta5
Cyan6
White7

Now to change the color of the text with the tput command, use the “setaf” option. Use the “setab” option of the tput command to modify the background color of the echo command. For example, we will change the background of the echo command’s output to “Yellow” and the text color to “White” by running the command:

$ echo "$(tput setaf 7) $(tput setab 3) Welcome to itsLinuxFoss$(tput sgr 0)"

This is all about the changing colors of the output of the echo command in Linux. 

Conclusion

Users can use the ANSI Escape Codes to alter the color of the output of the echo command. Both colors including the text and the background of the output of echo command can be changed using these codes. This post explained the usage of the ANSI codes for changing the color of the output of echo command. Additionally, the usage of the tput command for changing the color of the echo command has also been demonstrated with the example.