Comment

Bon Iver - RABi

135
Charles Johnson11/28/2019 10:09:52 am PST

In case anyone needs a bash function that captures stdout and stderr into separate variables and returns the command’s exit code as well.

catch() {
eval "$({
__2="$(
  { __1="$("${@:3}")"; } 2>&1;
  ret=$?;
  printf '%q=%q\n' "$1" "$__1" >&2;
  exit $ret
  )"
ret="$?";
printf '%s=%q\n' "$2" "$__2" >&2;
printf '( exit %q )' "$ret" >&2;
} 2>&1 )";
}

Pretty gnarly, I’d say.

You call it like this:

catch stdout stderr command