You're nearly there; you have a
:prologue set, via
exec 2>&1, to capture both stdout and stderr. To finish this, you just need two things:
:results: output to inform Babel that you want the results from what gets written out, not by what the code returns; and
:epilogue "true", to do what you're doing above with
|| true unilaterally and always without cluttering the code blocks.
#+BEGIN_SRC sh :exports both :dir /tmp :results output :prologue "exec 2>&1" :epilogue "true"
echo 'echo "$@" 1>&2;' > echoerr
chmod +x echoerr
./echoerr this is an error
false # guarantee failure
: this is an error
: sh: 6: nonesuch: not found
: Tue Apr 9 18:41:00 EDT 2019
Above, we've captured program error, shell error, and syntax error, as well as stdout, of course, all into
RESULTS. You can inject this into your
ob-shell environment via your preferred systemwide or per-file method rather than in each individual
#+BEGIN_SRC header (as you showed in your example above).
(Note, by the way, the quotes around the
:epilogue arguments; perhaps there's a versioning thing going on between us or something more subtle, but I couldn't get the effect you were getting with merely
:prologue exec 2>&1 without quotes.)
p.s. One thing you mustn't do, though:
return $NONZERO_VALUE. An explicit
return in the code block will short-circuit the rest of the block including the epilogue, and you'll fail execution again.