Colorize Terminal Output

This is part 2 of the 2nd week of my 100 Days of Code Challenge.

I'm somewhat of a plain styles kind of person. Color conveys a meaning that I don't often care for when it comes to things I create for myself. A white background with black for me is perfectly ok in most cases. When I'm particularly adventurous I may opt for a dark mode.

This week I decided that I'd add some color to some of my scripts. Since I was working on the backup script I decided to put some color into the info that the script output. It outputs simple pieces of text with echo just like this:

echo -e 'files archived'
echo -e 'starting db dump'

Different terminals use different colorization methods and I've colorized text a few times by Googling to find out how to make it happen. This time I done the same lol.

When I looked up the codes this time I got to a page with a very thorough breakdown of what can be done and how. It had this note at the top.

In Bash, the <Esc> character can be obtained with the following syntaxes:

  • \e
  • \033
  • \x1B

Seeing the list of escape characters reminded me of a time previously when I had added color in a python script. It output color in my terminal and in a terminal running on a Windows 10 machine. At the time I thought nothing about it.

Suddenly realizing that the color codes must be somewhat standardized I decided to look more closely. I discovered the codes come from a system called ANSI/VT100. A lot of terminals support it in some way or another. Windows added support for it in version 10. Prior to windows 10 there was no support.

Escape Color Sequences

Colorizing terminal output is done by using an escape sequence followed by a color code and then closed. Reset follows the same method so the codes end up looking something like these.

\e[1m # bold
\e[7m # inverted
\e[0m # reset

# OR with different escape sequences that bash understands. 

\033[1m # bold
\x1B[7m # inverted
\e[0m # reset

I have no idea exactly what the escape sequences that windows terminal supports however with my python script these were the colors I had used with the \033 escape sequence.

class tcolors:
    HEADER = '\033[95m'
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

You can see a large list of color codes and support for them on this page.

Color Escaped Text in a Bash Script

I decided to invert the colors of some of the info messages to make them stand out from the other messages that may scroll past. For a success messge I also turned the text green as well as doing the invert.

Those echo commands end up starting with the color code, outputting the text and then end with a reset code.

echo -e '\e[7m\e[92mfiles archived\e[0m'
echo -e '\e[7mstarting DB dump\e[0m'

Of course helper text and colorization is only useful in a manual run of this script. Since it's primary purpose is to be run as a cron job this info is going to be put to /dev/null. But I wanted to learn how it's done properly so that's what I did.