Bash pipe to stdout and stderr
The snippet can be accessed without any authentication.
Authored by
SHERMAN David
Bash function to run a command and send its stdout and stderr to two separate processes
Usage: pipe_tee_eval cmd-to-run cmd-for-stdout cmd-for-stderr
Any missing commands are treated as no-ops and their streams are discarded.
Example:
$ pipe_tee_eval '{ yes stdout | head; yes stderr | >&2 head; }' 'sed s/^/out:/' 'rev | sed s/^/err:/ | head -3'
out:stdout
out:stdout
out:stdout
out:stdout
out:stdout
out:stdout
out:stdout
out:stdout
out:stdout
out:stdout
err:rredts
err:rredts
err:rredts
pipe_tee_eval.bash 369 B
# Usage: pipe_tee_eval "cmd to run" "cmd for stdout" "cmd for stderr"
#
# Arguments will eval'ed by bash and so can contain shell redirections
# and variable interpolations.
#
# David Sherman 2021-07-24
# Plumbing from https://stackoverflow.com/a/31151808/9800033
pipe_tee_eval () {
{ eval "${1:-:}" 2>&1 1>&3 3>&- | eval "${3:-:}"; } 3>&1 1>&2 | eval "${2:-:}";
}
Please register or sign in to comment