views:

544

answers:

2

I have a problem that writes information to stdout and stderr, and I need to grep through what's coming to stderr, while disregarding stdout.

I can of course do it in 2 steps:

command > /dev/null 2> temp.file
grep 'something' temp.file

but I would prefer to be able to do is without temp files. Any smart piping trick?

+8  A: 

Redirect stderr to stdout and then stdout to /dev/null:

command 2>&1 >/dev/null | grep 'something'
Jonathan Leffler
+2  A: 

Or to swap the output from stderr and stdout over use:-

command 3>&1 1>&2 2>&3

This creates a new file descriptor (3) and assigns it to the same place as 1 (stdout), then assigns fd 1 (stdout) to the same place as fd 2 (stderr) and finally assigns fd 2 (stderr) to the same place as fd 3 (stdout). Stderr is now available as stdout and old stdout preserved in stderr. Maybe be overkill but hopefully gives more details on bash file descriptors (there are 9 available to each process).

Kramish