Sunday 14 April 2013

Swapping stdout and stderr [Shell Scripting]

In scripts it's often useful not to filter standard output (i.e. cmd | filter),
but stderr. This may be done using the following command:

    cmd 3>&1 1>&2 2>&3

It's even possible to filter both standard output and standard error:

    ( ( cmd | ... process stdout ) 3>&1 1>&2 2>&3 ) | \
        ... process stderr 3>&1 1>&2 2>&3

The last file descriptor operations restore the normal meaning of stdin and stdout.

0 blogger-disqus:

Post a Comment