How to change output color of echo in Linux
Learn, how to change the output color of echo text in linux command line.
The following example prints the foreground color of echo text to green and blue in the terminal.
echo "$(tput setaf 1)i am red $(tput setaf 4) i am blue"
setaf
is used to set the foreground color.
1
is the color code for red.
4
is the color code for blue.
Changing the background color
Similary, you can also change the background color of a echo text by using the setab
.
The following example prints the red background
with yellow
text.
echo "$(tput setaf 3) $(tput setab 1)How am i looking ?"
Color codes reference
These are the available color codes.
Num Colour #define R G B
0 black COLOR_BLACK 0,0,0
1 red COLOR_RED 1,0,0
2 green COLOR_GREEN 0,1,0
3 yellow COLOR_YELLOW 1,1,0
4 blue COLOR_BLUE 0,0,1
5 magenta COLOR_MAGENTA 1,0,1
6 cyan COLOR_CYAN 0,1,1
7 white COLOR_WHITE 1,1,1