There’s probably no Unix tool more useful than the venerable shell for automating tasks. Your scripts can become even more useful when you add c

How and Why You Should Add Color to Your Scripts

submited by
Style Pass
2023-05-25 18:30:07

There’s probably no Unix tool more useful than the venerable shell for automating tasks. Your scripts can become even more useful when you add color to their output to help humans pick out important information.

If you’ve added color to your scripts before, you may have used ANSI escape codes. The problem is that, when they’re not being used in a context that understands them, those codes can make output unreadable. Thankfully, this is a very solvable problem.

Colorizing and emphasizing terminal output goes way back. ANSI escape codes emerged in the 1970s as a standard way to do this — before then, different terminals all had different ways of doing things.

ANSI escape codes work by adding a special control code, beginning with an ESC character (often seen as “^[“), in line with the text. For example, changing the text color to red would be ESC followed by the string “[31m”.

The problem is that not everything that displays or captures the output of a command understands ANSI escape codes. If you’ve ever seen output littered with something like “^[[1mprocessing ^[[31msrc^[(B^[[m…”, you’ve experienced this.

Leave a Comment