[TriLUG] colorize stderr in bash

Kevin Hunter hunteke at earlham.edu
Wed Jul 23 17:19:51 EDT 2008


At 4:28p -0400 on Wed, 23 Jul 2008, Brian Phelps wrote:
> Is there an easy way to colorize stderr in an interactive bash shell?

Obligatory disclaimer: be wary of using colors in the terminal.  Not all
terminals/emulators are created equal, nor are ANSI escape sequences
portable.  Colorized output that looks absolutely splendid on one
author's machine may be barely readable on another's.  (e.g. Yellow text
on black, and white backgrounds.)  Also, be aware that a significant
portion of the population is colorblind, and for which colors have the
opposite effect they might have for you.

Long answer:

That said, I prefer colors and think they're currently underutilized in
the industry.  For those who *aren't* color blind, and who use ANSI
capable terminals emulators colors can help identify breaks in longs
scrolls of largely superfluous output text, or bring out warning or
error messages.  For example, a tail of a web log and highlighting 404s
(NOT FOUND), or 500s (SERVER ERROR)*, or parsing debug, informational,
and warning messages from a compiler, or running program.  Some folks
even colorize their command prompt.

Examples of programs in the wild that colorize (some) of their output
django, pulseaudio.  I have found that the lightred lines move my eye
directly where they need to be, and now I know what they mean; I don't
have to reread them everytime to know what's up.

If you're worried about portability and playing-nice-with-others, I
would caution you to make an *easy* opt-in or opt-out preference for
those who do(n't) want colors.

Short answer: the same way you'd do it with stdout: with escapes.

Some projects you might want to peruse:

http://scriptechocolor.sourceforge.net/
http://www.scribd.com/doc/2099456/Advanced-BashScripting-Guide

Kevin

* Obviously, you don't want to clutter the logs with escape sequences,
but what about piping to another program, e.g.
   $ tail -f log | http-colorizer



More information about the TriLUG mailing list