Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beccasaurus/806f990d38214c33960d3b1d671b71a7 to your computer and use it in GitHub Desktop.
Save beccasaurus/806f990d38214c33960d3b1d671b71a7 to your computer and use it in GitHub Desktop.
Get STDOUT and STDERR in Separate Variables
#! /usr/bin/env bash
foo() {
echo "STDOUT contents (called with args $*)"
echo "STDERR contents (called with args $*)" >&2
return 42
}
printOutBothStdoutAndStderrSeparately() {
echo "$({
error="$(
{ output="$( "$@" )"; } 2>&1;
ret=$?
declare -p output >&2
exit $?
)"
ret=$?
declare -p error
} 2>&1 )"
}
printOutBothStdoutAndStderrSeparately foo hello world
# Output:
# declare -- output="STDOUT contents (called with args hello world)"
# declare -- error="STDERR contents (called with args hello world)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment