I am trying to print a text in the terminal using echo command.
I want to print the text in a red color. How can I do that?
You should definitely use tput over raw ANSI control sequences.
Because there's a large number of different terminal control languages, usually a system has an intermediate communication layer. The real codes are looked up in a database for the currently detected terminal type and you give standardized requests to an API or (from the shell) to a command.
One of these commands is
tput
.tput
accepts a set of acronyms called capability names and any parameters, if appropriate, then looks up the correct escape sequences for the detected terminal in the terminfo database and prints the correct codes (the terminal hopefully understands).
from http://wiki.bash-hackers.org/scripting/terminalcodes
That said, I wrote a small helper library called bash-tint, which adds another layer on top of tput, making it even simpler to use (imho):
Example:
tint "white(Cyan(T)Magenta(I)Yellow(N)Black(T)) is bold(really) easy to use."
Would give the following result: