Immediately tell which output was sent to stderr

I just devised a crazy method involving FIFOs.

$ mkfifo foo
$ grep --color . foo &
$ your_command 2>foo

If you'd like the stderr output separate, you can open up two separate shells and run "grep --color . foo" in one without the &, then run the command in the other (still with the 2>foo). You'll get the stderr in the grep one and the stdout in the main one.

This works because the stderr output is routed through the FIFO into grep --color, whose default color is red (at least it is for me). When you're done, just rm the FIFO (rm foo).

Caveat: I'm really not sure how this will handle output order, you'll have to test it out.


Yes, this is possible. Look at the section "Make STDERR red" on this site for a working example.

The basic code is this

# Red STDERR
# rse <command string>
function rse()
{
    # We need to wrap each phrase of the command in quotes to preserve arguments that contain whitespace
    # Execute the command, swap STDOUT and STDERR, colour STDOUT, swap back
    ((eval $(for phrase in "$@"; do echo -n "'$phrase' "; done)) 3>&1 1>&2 2>&3 | sed -e "s/^\(.*\)$/$(echo -en \\033)[31;1m\1$(echo -en \\033)[0m/") 3>&1 1>&2 2>&3
}

A brief explanation is given in the function itself. What is does is move STDOUT and STDERR about, so sed gets STDERR in 1, colors it, and then swaps it back. Think of file stream 3 as a temp variable here.

The call is pretty simple

rse commands

However, certain invocations will not work as expected; the caveats are all provided on the linked page.

Btw, I think it is also possible to come with a solution of the form

commands | rse 

where rse will colorize the output.

I also found this hilite project that seems to do this. I haven't tried it out, but it might be what you're looking for.

hilite is a tiny utility which executes the command you specify, highlighting anything printed to stderr. It is designed mainly for use with builds, to make warnings and errors stick out like a sore cliche.

Other related projects:

  • color wrapper
  • colorshell
  • Script Echo Color
  • Colorifer. Check this too!

You can also check out stderred: https://github.com/sickill/stderred