How to Change the Output Color of Echo in Linux

Introduction

The echo command outputs a given string to the standard output pipe, typically pointing to the terminal. Although the standard output pipe can point to other interfaces - the echo command is typically used to print and display messages in the terminal. By default, the text of the displayed message inherits the color of other text in the terminal (which is customizable in and of itself). However - there are several ways you can alter the output color of echo - both for individual strings and for entire messages.

In this short guide, we'll take a look at how you can change the output color of echo in Linux-based systems, using ANSI escape codes, tput and how you can make this process less verbose in Bash scripts.

Change Output Color with ANSI Escape Codes

The easiest way to change the color is through ANSI escape sequences/codes. All ANSI escape codes start with the Escape character, which can be represented in various formats - 27 in decimal, \x1B in hexadecimal, as the control key ^[, or \033 in octal format. The sequences are then followed by the command:

\033[command

Where the opening bracket (Control Sequence Introducer) is optional, but helps separate the command from the escape character. When you put a color code as the command, it changes the color of the oncoming text:

\033[0;34

0;34 is the code for the color blue, for example. With this alone, you can change the color of text in echo with:

\033[0;34Text

Where Text would be colored blue. Alternatively, consider a simple bash script to print "Welcome to France" in the colors of the French flag:

#!/bin/bash  
BLUE='\033[0;34m'
WHITE= '\033[0;37m' 
RED= '\033[0;31m'   
echo -e "${Blue}Welcome ${WHITE}to ${RED}France"

The optional -e flag of the echo command allows you to use special characters like \n (newline) and \t (tab) inside of the input string.

Once you run the script:

$ ./colors.sh

It results in:

The ANSI codes aren't limited to color - but can also be applied for style. The codes 0..9 represent text styles, while the codes 30...37 represent colors:

Color Codes Text Styles Codes
Black 30 Simple text 0
Red 31 Bold text 1
Green 32 Low intensity text 2
Brown/Orange 33 Underline text 4
Blue 34 Blinking text 5
Purple 35 Invisible text 8
Cyan 36 Strikethrough text 9
Light Gray 37

Let's create a bash script to explore some of these options:

#!/bin/bash
echo -e "\033[0;33mSample text"
echo -e "\033[1;33mBold text"
echo -e "\033[2;33mLow intensity text"
echo -e "\033[4;33mUnderline text"
echo -e "\033[5;33mBlinking text"
echo -e "\033[8;33mInvisible text"
echo -e "\033[9;33mStrikethrough text"

Running this script results in:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

Similarly, you can change the background color of these texts using codes 40..47:

color Codes
Black 40
Red 41
Green 42
Brown/Orange 43
Blue 44
Purple 45
Cyan 46
Light Gray 47

Changing the background of a given string boils down to the same rule as when changing the font color - the code itself changes the behavior:

#!/bin/bash
BLUE='\033[0;44m'
BLACK='\033[0;30m'
WHITE='\033[0;30;47m'
RED='\033[0;41m'
echo -e "${BLUE}Welcome ${WHITE}to ${RED}France"

Note: As a rule of thumb - you can translate font colors to background colors by adding 10. 30 is black font color, 40 is black background color.

Change the Output Color with the tput Command

An alternative to ANSI codes is using the tput command:

tput setaf color_code

setf allows for 8 colors, while setaf allows for 256 colors so depending on the command you're using, you can go between 0..7 and 0..255 as the color codes. Both commands dedicate 0..7 to the same color codes, while with setaf, 8..15 are high-intensity colors, and 16..231 are different hues of the first 8, and 232..255 are grayscale colors:


Credit: Wikipedia

Finally, tput also allows you to change the background color, add bold, lower the intensity, etc. with other commands:

Text Styles Commands
Foreground color setaf
Background color setab
No style sgv0
Bold text bold
Low-intensity text dim
Underline text smul
Blinking text blink
Reverse text rev

Let's create another script that uses tput to change the output color echo:

#!/bin/bash
YELLOW=`tput setaf 3`
echo "${YELLOW}Changing"
WHITE=`tput setaf 7 bold`
echo "${WHITE}Colors"
BLUE=`tput setaf 4 smul`
echo "${BLUE}With tput"
CYAN=`tput setaf 6 blink`
echo "${CYAN}is less"
RED=`tput setab 1 setaf 7`
echo "${RED}verbose!"

This script will print the following output in the terminal:

The tput command delivers an easy way to print any color through a simple color code. Let's now create a script that can print every color code available for the tput command:

#!/bin/bash
tput init 
 end = $ (($ (tput colors) - 1)) 
 w = 1 
 for c
  in $ (seq 0 $end) 
  do
eval "$(printf " tput setaf % 3 s " " $c ")" 
      echo - n "$_" 
      [[$c - ge $ ((w * 2))]] 
      offset = 2 || offset = 0 
      [[$ (((c + offset) % (w - offset))) - eq $ (((w - offset) - 1))]] 
      echo 
 done 
 tput init

It will print 0 to 255 color and their codes:

Conclusion

In this short guide - we've taken a look at how you can change the color of echo's output in Linux - using ANSI Escape Sequences and the tput command.

We've also explored how you can make the process less verbose using Bash variables, tweak background colors and style text.

Last Updated: June 14th, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms