Changing Text Color in-Line?

Short question:

Using bash, is it possible to print a sentence such that each individual word has a different color?

I.e; print a word in-line, change the text color, repeat?

7

1 Answer

ANSI escape sequences

You can use ANSI escape sequences. It should work in text screens as well as most linux terminal window emulators.

See this link for details,

Example 1: White text on black background

echo -e "\0033[37;40m###############\0033[0m"

Example 2: Black text on greyish white background

echo -e "\0033[30;47m###############\0033[0m"

Example 3: Using the variables inversvid, greenback, blueback and resetvid

inversvid="\0033[7m"
resetvid="\0033[0m"
greenback="\0033[1;37;42m"
blueback="\0033[1;37;44m"
echo -e "$inversvid Now it is inverse colours $resetvid"
echo -e "$greenback Now it is greenback $resetvid and $blueback now blueback $resetvid"
Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.