Immediately tell which output was sent to stderr

Solution 1:

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

Solution 2:

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.

Solution 3:

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!

Solution 4:

Here is a lot of responses for highlighting the stderr output yet. I can only add one pretty easy - in one row, which you attach to the command:

command 2> >(while read line; do echo -e "\e[01;31m$line\e[0m"; done)

But you have to add this after command each time.

I, personally, like the possibility mentioned by @nagul , the rse function added to bashrc.

But I want to add the solution for printing the exit code. You can add this value to the beginning of your bash prompt row:

hostname$ command
  Some error occurred. Returned exit code.
EC hostname$

Where EC is exit code of the command.

I have it set in the way, that when the exit code is 0, it will be not printed, but any other value is printed before next prompt in red color.

The whole trick is done in ~/.bashrc:

my_prompt() {
 EXITSTATUS="$?"
 RED="\[\033[1;31m\]"
 OFF="\[\033[m\]"

PROMPT="${debian_chroot:+($debian_chroot)}\h \$ "

if [ "${EXITSTATUS}" -eq 0 ]; then
   PS1="${PROMPT}"
else
   PS1="${RED}$EXITSTATUS${OFF} ${PROMPT}"
fi
}

PROMPT_COMMAND=my_prompt

The prompt line is by default defined by PS1 variable. Copy whatever you have here into the variable PROMPT and then create the PS1 variable with or without exit code.

Bash will show the information in PS1 variable in the prompt.

Solution 5:

there's annotate-output utility (devscripts Debian package), if you would like to do it without colors

$ annotate-output /tmp/test.py    
14:24:57 I: Started /tmp/test.py
14:24:57 E: this is stderr
14:24:57 O: this is stdout
14:24:57 O: this is stdout again
14:24:57 I: Finished with exitcode 0