Skip to content

Instantly share code, notes, and snippets.

@Peeja
Created March 13, 2013 15:02
Show Gist options
  • Save Peeja/5152969 to your computer and use it in GitHub Desktop.
Save Peeja/5152969 to your computer and use it in GitHub Desktop.
Moving STDERR and STDOUT around for great justice.
# A program which outputs "error" to STDERR and "output" to STDOUT.
a_program() {
echo error >&2
echo output
}
# Run a_program.
# - Redirect a_program's STDERR (file descriptor 2) to the STDIN of a tee process.
# - tee echoes its STDIN to the given filename and to its STDOUT.
# - Redirect tee's STDOUT to its STDERR.
a_program 2> >(tee errors.txt >&2)
# The upshot: All output on a_program's STDOUT will end up in errors.txt and on the whole pipeline's STDERR.
error_processor() {
(echo "Got errors: "; cat) | say
}
# Do the above, but send errors to error_processor
a_program 2> >(tee >(error_processor) >&2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment