Redirecting stderr in org-mode shell blocks

| categories: orgmode | tags:

Org-mode shell source blocks do not capture stderr. For example, in this block, with the default setup:

echo "testing stdout" >&1
echo "testing stderr" >&2
testing stdout

You can see the second line is not in the output.

If you run this command, you get an Org-Babel Error Output buffer saying it is an illegal option.

date -g

It would be nice to just capture that error, and show it.

We solved this problem in Python by redirecting stderr at runtime. It is not that simple in shell blocks, but we can do a similar thing. The code block is executed (I think) by saving the block to a temporary file, and then running org-babel-sh-command on the file. That magic happens inside the command shell-command-on-region. We just need to make that command redirect stderr. Here is a new shell command that does this. This next block can be tangled out to an executable command. This script takes one argument, which I believe is a filename (the temporary file containing the source block region).

#!/bin/bash
{
bash $1
} 2>&1

Now, we set org-babel-sh-command to our new shell command.

(setq org-babel-sh-command "./sh_stderr.sh")
./sh_stderr.sh

Now, it appears we get what we want:

echo "testing stdout" >&1
echo "testing stderr" >&2
testing stdout
testing stderr

And, with the bad option to date, we get:

date -g
echo
date: illegal option -- g
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

Not bad! I have not tested this very thoroughly, i.e. beyond these little examples, but it seems like it could work.

Achim Gratz suggested this simpler approach that does not involve any external scripts. The : at the end is important!

exec 2>&1
echo "testing stdout" >&1
echo "testing stderr" >&2
date -g
:
testing stdout
testing stderr
date: illegal option -- g
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

Copyright (C) 2015 by John Kitchin. See the License for information about copying.

org-mode source

Org-mode version = 8.2.10

Discuss on Twitter