Changing text colour in bash

I am doing a basic script to check if services are disabled, and I was wondering how to change to colours for PASS and FAIL to green & red respectively.

#!/usr/bin/bash

clear


TELNET=`svcs -a | grep telnet | awk '{print $1}'`

if [ "$TELNET" == "disabled" ]
then
        RESULT=PASS
else
    RESULT=FAIL
fi
echo -e "Verifying if Telnet services are disabled                [$RESULT]"


FTP=`svcs -a | grep ftp | awk '{print $1}'`

if [ "$FTP" == "disabled" ]
then
    RESULT2=PASS
else
    RESULT2=FAIL

fi
echo "Verifying if FTP services are disabled                    [$RESULT2]"

Use "tput"

tput setaf 2
echo Green
tput setaf 1
echo Red
tput setaf 7
echo normal

See man tput and the "Color Handling" section of "man terminfo"

---------- Post updated at 05:33 AM ---------- Previous update was at 05:20 AM ----------

Oh, and by the way:

I wouldn't do that unless you are the only one to use the script.

Color is tricky. Displays can be bad at it, people can be color blind...

I might do this:

echo "`tput setaf 1` **** `tput setaf 7` FAIL  `tput setaf 1` *** `tput setaf 7`"

which leaves FAIL in white.

But even at that you have to be trickier. This would be awful on a white background - "FAIL" would be invisible.

I honestly don't like colors for scripts.

That doesn't work on at least one of the systems I use. There are two different versions of tput. One uses termcap, the other terminfo. They are not compatible.

On the other hand, the ISO 6429 standard (also known as ECMA-48, and formerly known as ANSI X3.64), is ubiquitous, and terminals that do not support it are few and far between.

green=$(printf "\033[32m")
normal=$(printf "\033[m")

Generally, I agree with you, but there are times when it is useful.

Yeah, I tend to forget that nobody thinks of or uses anything but Ansi nowadays :slight_smile: