Change Output Color for Echo Command

How To Change The Output Color Of Echo Command

In this article, we’ll learn how to change the output color for echo command. The echo command simply writes a text in the terminal. For example,

echo ‘Hello World’

Will simply print ‘Hello World’ in the terminal. In this article, we’ll learn how to change the output color for the echo command.

Output Of Echo Command
Output Of Echo Command

This command can be really useful in writing bash scripts, as you can have it programmed in such a way that it’ll give certain output to let you know that a task has been completed. In this article, we will show you how to change the color of the output of the echo command so that certain words or sentences can be highlighted.

ANSI Escape Codes to Change Output Color of echo Command

First, we need to learn about what ANSI escape codes are. ANSI escape sequences are essentially the tools through which cursor location, color, font styling, and other options can be controlled on the terminal. The terminal interprets these codes as commands. All you need to know about this are the ANSI codes for colors.

Black0;30Dark Gray1;30
Red0;31Light Red1;31
Green0;32Light Green1;32
Blue0;34Light Blue1;34
Purple0;35Light Purple1;35
Cyan0;36Light Cyan1;36
Light Gray0;37White1;37
ANSI codes for colors

Now, we that we have the ANSI codes, we can use it in the terminal as follows:

# No Color
echo -e "${YELLOW}Linux for Devices is${CYAN} the best!"
Changed Color Of Echo Command
Changed Color Of Echo Command

Change color with the help of Tput command

You can also use the Tput command to set the font color and background color of your echo command output. Use the ‘setaf’ option of tput command to set the font color :

tput setaf 6; echo "This is Cyan colored text"
Basic Output Of Tput Command
Basic Output Of Tput Command

This will simply change the font color to Cyan. To avoid typing out the whole command, again and again, you can also choose to write:

red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
echo "${red}This text is Red ${green}This Text is Green ${reset}"
Red And Green Text Using Tput
Red And Green Text Using Tput

Here’s a table of all the colors supported by the Tput command :

Colors supported by Tput command

You can also change the background of the echo output by simply using ‘setab’ instead of ‘setaf’, you can also use both sub-commands in combination. For example:

echo "$(tput setaf 1)Red text $(tput setab 3)and Yellow background$(tput sgr 0)"
Set Background Color using 'setab'
Set Background Color using ‘setab’

Just like with the ANSI codes, we can also set up our own custom variables so that we don’t have to type it out the whole command every time. You can also turn the text in bold or have it underlined. Here are some variables which you can use :

# Reset
Color_Off='\033[0m'       # Text Reset

# Regular Colors
Black='\033[0;30m'        # Black
Red='\033[0;31m'          # Red
Green='\033[0;32m'        # Green
Yellow='\033[0;33m'       # Yellow
Blue='\033[0;34m'         # Blue
Purple='\033[0;35m'       # Purple
Cyan='\033[0;36m'         # Cyan
White='\033[0;37m'        # White

# Bold
BBlack='\033[1;30m'       # Black
BRed='\033[1;31m'         # Red
BGreen='\033[1;32m'       # Green
BYellow='\033[1;33m'      # Yellow
BBlue='\033[1;34m'        # Blue
BPurple='\033[1;35m'      # Purple
BCyan='\033[1;36m'        # Cyan
BWhite='\033[1;37m'       # White

# Underline
UBlack='\033[4;30m'       # Black
URed='\033[4;31m'         # Red
UGreen='\033[4;32m'       # Green
UYellow='\033[4;33m'      # Yellow
UBlue='\033[4;34m'        # Blue
UPurple='\033[4;35m'      # Purple
UCyan='\033[4;36m'        # Cyan
UWhite='\033[4;37m'       # White

# Background
BG_Black='\033[40m'       # Black
BG_Red='\033[41m'         # Red
BG_Green='\033[42m'       # Green
BG_Yellow='\033[43m'      # Yellow
BG_Blue='\033[44m'        # Blue
BG_Purple='\033[45m'      # Purple
BG_Cyan='\033[46m'        # Cyan
BG_White='\033[47m'       # White

# High Intensity
HIBlack='\033[0;90m'       # Black
HIRed='\033[0;91m'         # Red
HIGreen='\033[0;92m'       # Green
HIYellow='\033[0;93m'      # Yellow
HIBlue='\033[0;94m'        # Blue
HIPurple='\033[0;95m'      # Purple
HICyan='\033[0;96m'        # Cyan
HIWhite='\033[0;97m'       # White

# Bold High Intensity
BIBlack='\033[1;90m'      # Black
BIRed='\033[1;91m'        # Red
BIGreen='\033[1;92m'      # Green
BIYellow='\033[1;93m'     # Yellow
BIBlue='\033[1;94m'       # Blue
BIPurple='\033[1;95m'     # Purple
BICyan='\033[1;96m'       # Cyan
BIWhite='\033[1;97m'      # White

# High Intensity backgrounds
BG_IBlack='\033[0;100m'   # Black
BG_IRed='\033[0;101m'     # Red
BG_IGreen='\033[0;102m'   # Green
BG_IYellow='\033[0;103m'  # Yellow
BG_IBlue='\033[0;104m'    # Blue
BG_IPurple='\033[0;105m'  # Purple
BG_ICyan='\033[0;106m'    # Cyan
BG_IWhite='\033[0;107m'   # White


We hope that using this article, you were able to learn how to change the echo command output color and make your document to make your code or script even more beautiful.