[TriLUG] colorize stderr in bash

Andrew Ball anball at gmail.com
Wed Jul 23 17:40:04 EDT 2008


I wonder if there's a way to pipe stderr to a program that adds
escape sequences and sends it right back to stderr while
leaving stdout intact.


On Wed, Jul 23, 2008 at 5:37 PM, Brian Phelps <brphelps at ieee.org> wrote:
> Thats interesting, not worried about doing it with files or certain
> commands, I think I can figure out how to do that with sed.
>
> I guess I'm looking for the short quick answer to "What line can I add
> to .bashrc to get colorized stderr"
>
> I found one for zcsh, but I don't know how to get my shell, bash to do
> something similar when I log in interactively.
>
>
> On Wed, 2008-07-23 at 17:19 -0400, Kevin Hunter wrote:
>> 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.
>>
> How do I get the shell to do it interactively?
>
>> 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
> --
> <a href=http://electronjunkie.wordpress.com>Got e-?</a>
>
> --
> TriLUG mailing list        : http://www.trilug.org/mailman/listinfo/trilug
> TriLUG FAQ  : http://www.trilug.org/wiki/Frequently_Asked_Questions
>



-- 
=======================
Andrew D. Ball
勃安
deus caritas est
http://www.ibiblio.org/adball/blog/
http://filebox.vt.edu/~anball1/


More information about the TriLUG mailing list