BASH Programming

How to Change the Output Color of Echo in Bash – Linux

The echo command is widely used in bash scripting. The echo command prints a string or message in the terminal.

In bash scripting to differentiate different output messages the color of the echo string can be changed. This article covers the commands needed to change the color of the output of echo in Linux.

How to Change the Color of echo in Linux

The easiest method to change the output color of echo command is using the ANSI escape codes.

ANSI escape codes are used to modify the output of echo and printf commands. ANSI codes are started with escape character which are mentioned below:

\x1B Hexadecimal
\033 Octal

The octal escape code (\033) is mostly used. The syntax of using ANSI escape codes is:

\033[<code>m

In the above syntax the <code> will be replaced with the ANSI color code.

CSI: Control Sequence Inducer

Some most used ANSI escape codes are listed in the table below:

Black 0;30
Red 0;31
Green 0;32
Orange 0;33
Blue 0;34
Purple 0;35
Cyan 0;36
Light Grey 0;37

For light shades of the colors the ANSI codes are mentioned below:

Dark Grey 1;30
Light Red 1;31
Light Green 1;32
Yellow 1;33
Light Blue 1;34
Light Purple 1;35
Light Cyan 1;36
White 1;37

The syntax to change the color of a particular string would be:

\033[0;31<TEXT>m

The above syntax will change the <TEXT> color to Red.

How to Change Color of Echo Output Bash – Linux

Now, let’s create a simple bash script to change the text color of echo in Linux.

Create a bash script file using the command given below:

$ sudo nano mybashScript.sh

Now, type the script, an example script is given below:

#!/bin/bash

red='\033[0;31m'

green='\033[0;32m'

blue='\033[0;34m'

echo -e "{red}This is a bash script"

echo -e "${green}Hello ${blue}LinuxHint"

The -e flag is inserted with echo command to use the escape sequence.

To run the script, use the command mentioned below:

$ bash mybashScript.sh

How to Change Background Color of Echo Output Bash – Linux

ANSI codes are not limited to change the text color, it can also be used to apply background color to a text.

To apply the background color to echo output the method is quite similar but ANSI codes will be different.

The list of codes to change the background color is given below:

Black 0;40
Red 0;41
Green 0;42
Orange 0;43
Blue 0;44
Purple 0;45
Cyan 0;46
Light Grey 0;47

Let’s understand it with a bash script example:

#!/bin/bash

grey='\033[0;47m'

cyan='\033[0;46m'

echo -e "${grey}Hello ${cyan}LinuxHint"

Conclusion

The echo command is used to print text in the terminal. To distinguish output messages different colors can be applied to the echo output. The ANSI escape codes are used with echo that change output color. These codes can change the text along with its background.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.