Richard Richard - 1 year ago 60
Linux Question

IO Redirection - Swapping stdout and stderr (Advanced)

Given a shell script:


echo "I'm stdout";
echo "I'm stderr" >&2;

Is there a way to call that script such that only stderr would print out, when the last part of the command is 2>/dev/null, ie

$ > sh SOME_OPTIONS_HERE 2>/dev/null
I'm stderr

Or, alternatively:

$ > sh SOME_OPTIONS_HERE >/dev/null
I'm stdout

It's a question at the end of a set of lecture slides, but after nearly a day working at this, I'm nearly certain it's some sort of typo. Pivoting doesn't work. 2>&- doesn't work. I'm out of ideas!

Answer Source
% (sh 3>&2 2>&1 1>&3) 2>/dev/null
I'm stderr
% (sh 3>&2 2>&1 1>&3) >/dev/null 
I'm stdout
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download