by Sai gowtham

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"

bash echo change color

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

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course