3

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.

Thomas Dickey
  • 75,040
  • 9
  • 171
  • 268

2 Answers2

3

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"
Toby Speight
  • 8,460
  • 3
  • 26
  • 50
2
echo -e "\e[31minvalid entries\e[0m"

Reference: Bash tips: Colors and formatting (ANSI/VT100 Control sequences)

neuron
  • 1,941
  • 11
  • 20