For testing purposes, I would like to save stdout and stderr separately for inspection by subsequent code. For example, a test run with erroneous input should resul
For the last iteration, where the value of bar is set to the contents of stderr, try:
# restore original stdout & stderr at the end by adding: 1>&2 2>&1 bar="$( { { echo foo >&2 | tee stdout.log 2>&3 3>&- } 2>&1 >&4 4>&- | tee stderr.log 2>&3 3>&- } 3>&2 4>&1 1>&2 2>&1 )" echo "$bar"