Skip to main content
Quick Tip

Changing the Color of the echo Command's Output

Using colors with the echo command could make your shell scripts more user friendly and attractive.

Team LHB

Warp Terminal

In UNIX-like operating systems, the echo command is used to display a string of characters onto the terminal.

Usually, the color of output of the echo command follows the terminal theme, nothing unusual about that.

But if you want, you can change the color of the output text of the echo command in Linux.

Why would you want to do that? There could be several reasons for that. For examples, if you are writing a script that has a step where it needs to warn the user, using red colored output could be useful.

Using colored output in echo command

Let me tell you how you can change the text color from either black or white to something else entirely, like yellow, red, green.

ANSI escape codes for the colored output

To change the output color of echo command, you have to use escape sequences.

A popular example of an escape sequence that you might have used in the past is what we use for a newline, '\n'. Similarly, there are escape sequences for specifying colorized output as well.

Below are a few ANSI escape codes for each color:

Black        0;30     Dark Gray     1;30
Red          0;31     Light Red     1;31
Green        0;32     Light Green   1;32
Brown/Orange 0;33     Yellow        1;33
Blue         0;34     Light Blue    1;34
Purple       0;35     Light Purple  1;35
Cyan         0;36     Light Cyan    1;36
Light Gray   0;37     White         1;37

You can use these escape codes with '\033[', followed by the ANSI escape code of the color of your choice, and finally a 'm'.

Below are a few examples how to string the ANSI color code together:

R='\033[0;31m'   #'0;31' is Red's ANSI color code
G='\033[0;32m'   #'0;32' is Green's ANSI color code
Y='\033[1;32m'   #'1;32' is Yellow's ANSI color code
B='\033[0;34m'   #'0;34' is Blue's ANSI color code

The usage of this escape character is similar to how you use the backslash '\' with 'n' to signify a newline.

Colorized output in echo command

You can use the specified colors as a “variable” in the echo command and mention it before any text to make the text appear with said color in the terminal output.

Whenever you use any special escape sequence like '\n', '\t', or these escape sequences for colors with echo command, please make sure to use the "-e" flag with the echo command.

In this example, I have declared two variables with color codes. The first one is for Red and the 'NOCOLOR' is to stop red and switch back to the default color.

As you can see, it displayed the text 'love' in red. I used it directly in the shell. You can put it in script in the same manner:


echo -e "I ${RED}love${NOCOLOR} Linux"

Here is another example with more colors in the echo command output:

This time, since the last character in the string of text needs to be in yellow, it is okay to not switch back to the regular color by the use of 'NOCOLOR'.

You can add color to the output of printf as well

If you want a formatted string, you are better off using the printf command instead of the echo command.

Once you have defined the color's escape sequences - which are exactly the same as you used for echo, you can use it with printf as well.

Here, I have done the same thing I did for echo, I specified and used the 'NOCOLOR' variable to stop using the red color and switch back to default color after the word 'love'.

Bonus Tip: Use an online tool to generate colors

GitHub user Gbox4 created an extremely helpful website called 'ANSI code generator' that helps you generate these escape sequences for you.

It lets you select from a variety of colors for the text background and for the text foreground (text color), styles - bold, italic, underline or strikethrough.

Then you can easily store that automatically generated code into a bash variable for future use.

ANSI escape code generator


Now that you know how to add colors to echo command output, you can make your bash scripts more useful and attractive.

Since the printf command is more predictable, allows formatting our output and highly configurable, if you have a specific agenda, I would recommend going the printf route.

If you are achieving a simple task, use the echo command.

Team LHB