Use Colored Output With the echo Command
Add some colors to your scripts by using colored output with the echo command.
The echo command is mainly piped to a command to print a statement to a specific case or in scripts where you want to make it interactive or to print output.
But have you ever thought of changing the colors of the echo command?
Looks cool. Right? Imagine that you have a script that needs to warn the users at some step. Using red colored text would be wise here.
There could be many more practical usecases of using color with echo command output. I won't discuss them all. I'll show you how you can get the colors with the echo command.
How to get colored output with the echo command
As always, you have multiple ways to accomplish the same task in Linux.
And in this tutorial, I'm going to walk you through two methods to use colored output with the echo command:
- Using ASCII color codes (has more options)
- Using the tput command
So let's start with the first one.
1. Using ASCII color codes
In this section, I will show you how you can create a variable containing the ASCII color code and use it to get colored output with the echo command.
But before that, you need to learn some basic ASCII color codes that will be used to color the output:
Code | Color | Code | Color |
---|---|---|---|
0;30 |
Black | 1;30 |
Dark Gray |
0;31 |
Red | 1;31 |
Light Red |
0;32 |
Green | 1;32 |
Light Green |
0;33 |
Brown/Orange | 1;33 |
Yellow |
0;34 |
Blue | 1;34 |
Light Blue |
0;35 |
Purple | 1;35 |
Light Purple |
0;36 |
Cyan | 1;36 |
Light Cyan |
0;37 |
Light Gray | 1;37 |
White |
And to use it in our case, you have to pair it with \033[
which is ASCII escape character.
Let's suppose you want to use the red color, then, the code combining the ASCII escape and color would be '0\33[0;31'
. But you'd also have to add a delimiter to specify the end of the code.
So with the delimiter, our code for the red color would be '\33[0;31m'
. Here, the delimiter was m
.
Once you have a code, you can create a variable containing the value.
To create a variable, you follow the given syntax:
NAME_OF_VAR='code'
As I want to create a variable for the red color, I would configure it in the following way:
RED='\033[0;31m'
After that, all you have to do is implement the color in the echo:
echo -e "${ENV_VAR} Text that'd follow the color scheme"
Here's what I had with my configuration:
Similarly, you can add multiple colors too!
To do so, you'd have to follow the given syntax:
echo -e "${ENV_VAR1}Color of ENV_VAR1 ${ENV_VAR2}Color of ENV_VAR2"
In simple terms, the color will be applied to the text followed by the variable and will be changed until you add another color.
For example, here, I used two colors: Red and Blue:
2. Using the tput command
If you don't want to remember those complex ASCII color codes, then, this can be a perfect fit for you!
First, let's have a look at the color codes of the tput command:
Code | Color | Code | Color |
---|---|---|---|
0 |
Black | 4 |
Blue |
1 |
Red | 5 |
Magenta |
2 |
Green | 6 |
Cyan |
3 |
Yellow | 7 |
White |
You can use the tput command in two ways:
tput setaf [color_code]
: Define color for text.tput setab [color_code]
: Define color for the text background
tput sgr 0
at the end of the line which will work as a delimiter for the background.And this is the syntax that you have to follow to use the tput command:
tput setaf/setab <color_code>; echo "This is colored text"
For example, here, I used yellow text:
tput setaf 3; echo "This is yellow colored text"
But if you want to use multiple colors, you can create variables for the tput command values by following the given syntax:
VAR=`tput setaf color_code`
For example, here, I created a color code for the RED color:
RED=`tput setaf 1`
And used it in the simple sentence:
echo "${RED}This text is Red"
You can also use multiple colors and use them simultaneously in the text:
VAR1=`tput setaf color_code1`
VAR2=`tput setaf color_code2`
echo "${VAR1}Color from VAR1 ${VAR2}Color from VAR2"
For example, here, I used yellow for the text color and green for the background color:
Green=`tput setaf 2`
Yellow=`tput setab 3`
Delimiter=`tput sgr 0`
echo "${Green}${Yellow}Green text with the Yellow background.${Delimiter}"
Pretty cool. Right?
You can colorize the diff command too!
If you don't know, the diff command is used to find the difference between two files, and when colored, it becomes much more readable!
Want to know how to do that? Here's the detailed guide:
I hope, you will find this guide helpful!
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.