Bash: write (echo) to stderr

This is a wiki page. Be bold and improve it!

If you have any questions about the content on this page, don't hesitate to open a new ticket and we'll do our best to assist you.

3.6.4 Redirecting Standard Output and Standard Error
http://www.gnu.org/software/bash/manual/bash.html#Redirecting-Standard-O...

#!/bin/bash
# Both are OK:
echo "WARNING! My error message." 1&>2
1&>2 echo "WARNING! My error message."

Note that &>2 is the same as 1&>2 since 1 is stdout and is the default output channel.

Or you could define a function:

#!/bin/bash
print_error() {
   echo "$@" 1>&2;
}

print_error "WARNING! My error message."

The following function would print everything passed to it:

print_error() {
    echo "$@" 1>&2;
}

stderred

https://github.com/sickill/stderred
stderred hooks on write() and a family of stream functions (fwrite, fprintf, error...) from libc in order to colorize all stderr output that goes to terminal thus making it distinguishable from stdout. Basically it wraps text that goes to file with descriptor "2" with proper ANSI escape codes making text red.