I was trying out the answer to this question (the portion that is bash <= 4 compatible) when I noticed that bash behaved differently if the following line was in or out of a function. When in a function, the variables created by the eval
were local variables, which is odd because there is no local keyword in the eval
string.
eval "$({ berr=$({ bout=$("$@"); bret=$?; } 2>&1; declare -p bout bret >&2); declare -p berr; } 2>&1)"
function captureStdOutRetCode () {
eval "$({ berr=$({ bout=$("$@"); bret=$?; } 2>&1; declare -p bout bret >&2); declare -p berr; } 2>&1)"
}
echo "_${bout}_${berr}_${bret}_" # ____
My end goal is to port this to something that is compatible with ash
and is callable as a function. My plan is to replace the declare
bits with echo "bout='$bout'"
since in my case I know the command's output is safe to eval
without the escaping that declare
provides (or would provide in bash
at least).