How to change shell echo command color in linux

Last updated on February 21st, 2022 at 07:47 am

Click to rate this tutorial!
[Total: 0 Average: 0]

We are going to discuss in detail how to change output color of shell echo command in linux,

This is list of color codes for mostly used colors like White, Red, Yellow, Grey etc.,

Black       0;30     Dark Gray     1;30
Blue        0;34     Light Blue    1;34
Green       0;32     Light Green   1;32
Cyan        0;36     Light Cyan    1;36
Red         0;31     Light Red     1;31
Purple      0;35     Light Purple  1;35
Brown       0;33     Yellow        1;33
Light Gray  0;37     White         1;37

How to implement this?

It is very simple, Inside your shell script declare these values first

For example

#!/bin/sh
red='\e[0;31m';
NC='\e[0m';
yellow='\e[1;33m';
green='\e[0;32m'

Then use echo command to display the result. Dont forget to put -e switch with the echo command. This will enable interpretation of backslash escapes.

So a sample script will look like this

#!/bin/sh
red='\e[0;31m';
NC='\e[0m';
yellow='\e[1;33m';
green='\e[0;32m'
website='mistonline.in';
echo "This is a test message from ${red}$website${NC}";

Output:

$ ./run.sh
This is a test message from mistonline.in

Make sure you put ${NC} just before the closing double quotes(“) in the echo -e command, this is nothing but NO COLOR option which will display your default putty font color after displaying the content in the echo command. Otherwise you will see a change of color for your putty font, But dont worry that will be only for your current session. Once you close and reopen putty it will set to default font color.

Click to rate this tutorial!
[Total: 0 Average: 0]

Leave a Reply

Your email address will not be published.