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?
My favourite answer so far is coloredEcho.
Just to post another option, you can check out this little tool xcol
https://ownyourbits.com/2017/01/23/colorize-your-stdout-with-xcol/
you use it just like grep, and it will colorize its stdin with a different color for each argument, for instance
sudo netstat -putan | xcol httpd sshd dnsmasq pulseaudio conky tor Telegram firefox "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+" ":[[:digit:]]+" "tcp." "udp." LISTEN ESTABLISHED TIME_WAIT
Note that it accepts any regular expression that sed will accept.
This tool uses the following definitions
#normal=$(tput sgr0) # normal text
normal=$'\e[0m' # (works better sometimes)
bold=$(tput bold) # make colors bold/bright
red="$bold$(tput setaf 1)" # bright red text
green=$(tput setaf 2) # dim green text
fawn=$(tput setaf 3); beige="$fawn" # dark yellow text
yellow="$bold$fawn" # bright yellow text
darkblue=$(tput setaf 4) # dim blue text
blue="$bold$darkblue" # bright blue text
purple=$(tput setaf 5); magenta="$purple" # magenta text
pink="$bold$purple" # bright magenta text
darkcyan=$(tput setaf 6) # dim cyan text
cyan="$bold$darkcyan" # bright cyan text
gray=$(tput setaf 7) # dim white text
darkgray="$bold"$(tput setaf 0) # bold black = dark gray text
white="$bold$gray" # bright white text
I use these variables in my scripts like so
echo "${red}hello ${yellow}this is ${green}coloured${normal}"