FLOX Advocate · @FLOX_advocate
449 followers · 6343 posts · Server floss.social

Capture STDOUT, STDERR and return status in shell variables

This second answer ( the one with the banana function ) shows how to capture all three to variables in bash without needing tmp files

stackoverflow.com/questions/13

----
banana() {
echo "banana to stdout"
echo >&2 "banana to stderr"
return 42
}
----

----
. <({ berr=$({ bout=$(banana); bret=$?; } 2>&1; declare -p bout bret >&2); declare -p berr; } 2>&1)
----

Then check values of $bout, $berr, $bret

#flox #bash #ShellTricks

Last updated 4 years ago