This is just an expansion on the great answer from @EdMorton for anyone who wants to wrap it, as a a proof of concept this works:
#!/bin/bash
func() {
echo "test...stdout - arg 1: $1 - arg 2: $2"
echo "test...stderr 1" >&2
echo "test...usrerr" >&4
echo "test...stderr 2" >&2
return 1
}
wrap() {
func_name=$1; shift; func_args=$@
{ em=$($func_name $func_args 4>&2 2>&1 1>&3-); ec=$?; } 3>&1
if [[ ec != 0 ]]; then
echo "Error code: $ec"
echo "Captured stderr (not shown):"
echo "$em"
else
echo "There were no errors."
fi
}
wrap func abc xyz
And the result is:
test...stdout - arg 1: abc - arg 2: xyz
test...usrerr
Error code: 1
Captured stderr (not shown):
test...stderr 1
test...stderr 2