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?
Use tput
to calculate color codes. Avoid using the ANSI escape code (e.g. \E[31;1m
for red) because it's less portable. Bash on OS X, for example, does not support it.
BLACK=`tput setaf 0`
RED=`tput setaf 1`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
BLUE=`tput setaf 4`
MAGENTA=`tput setaf 5`
CYAN=`tput setaf 6`
WHITE=`tput setaf 7`
BOLD=`tput bold`
RESET=`tput sgr0`
echo -e "hello ${RED}some red text${RESET} world"