Shell – change colour of `echo` for particular line in shell script in Linux

ansi-termechoescape-charactersshell-script

I want to change the colour of echo for a particular statement in a Linux shell script

example :

  echo "invalid entries"
  echo "valid entries"
  echo "valid entry"

I want the red color for echo "invalid entries" statement; the rest of them should be same as the default color.

Best Answer

Use tput to get the control sequences (if they exist) for the user's terminal:

red="`tput setaf 1`"
green="`tput setaf 2`"
cyan="`tput setaf 6`"
bold="`tput bold`"
norm="`tput sgr0`"

echo "${red}invalid entries${norm}"
echo "valid entries"
echo "valid entry"
Related Question