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?
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,
en.wikipedia.org/wiki/ANSI_escape_code
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"
Declare and store variables
Example of basic ANSI colour variables, that I use in bash shellscripts, and that you might find useful,
inversvid="\0033[7m"
resetvid="\0033[0m"
redback="\0033[1;37;41m"
greenback="\0033[1;37;42m"
blueback="\0033[1;37;44m"
Example of advanced ANSI colour variable (that almost matches the mkusb logo colour),
logoansi="\0033[38;5;0;48;5;148m"
The advanced ANSI colours work in most terminal window emulators, but not in text screens, where the colour defaults to 'the nearest basic colour'.
- It is straightforward to declare and store the variables in a bash shellscript (near the beginning, at least before they are used).
- If you want to use them interactively, you can declare and store the variables in the configuration file
~/.bashrc
And of course, you can create [modified] variables to perform what you want.