June 3, 2009

Swapping STDOUT and STDERR under Bash Shell

I usually forgot this simple command so I've decided to write down some notes on it so I can cut 'n' paste from here easily, here's another common question:

"How to swap stdout and stderr descriptors ?" 

This enables your scripts to parse stderr and forget stdout (or redirect it to a file)

Here's:

command <parameters> 3>&1 1>/dev/null 2>&3-

Here's what it does:

Moves stdout to /dev/null (or your preferred file if needed).
Moves stderr to file descriptor (3)
Moves file descriptor (3) (containing stderr) to stdout (real stdout, not null!)

 

This is what I use to parse errors with my scripts when I got something bad from external programs

 

Glad to see comments

Ben 

Click Here!