Last active
May 7, 2021 18:24
-
-
Save beccasaurus/806f990d38214c33960d3b1d671b71a7 to your computer and use it in GitHub Desktop.
Get STDOUT and STDERR in Separate Variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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