How can I redirect STDERR to STDOUT, but ignore the original STDOUT? [duplicate]

Solution 1:

What does not work:

The reason the last command you quoted:

cmd 1>/dev/null 2>&1 | grep pattern

does not work, stems from a confusion on the order in which redirection works. You expected the last quoted redirection to be applied to the ones before it on every output, so that output the original standard output file descriptor (1) will go to /dev/null, and output to the standard error file descriptor (2) will go to the original standard output.

However, this is not how shell redirection works. Each redirection causes the file descriptors to be "remapped" by closing the "source" and duplicating the "destination" into it (see the man pages of dup(2) and close(2)), in order. This means that in your command standard output is first replaced with /dev/null, and then standard error replaced with standard output, which is /dev/null already.

What works:

Therefore, to obtain the desired effect, you just need to reverse the redirections. Then you will have standard error go to standard output, and the original standard output go to /dev/null:

cmd 2>&1 >/dev/null | grep pattern

(note that the 1 before > is unnecessary - for output redirection standard output is the default)


Addendum: Charlie mentioned redirecting to &- to close a file descriptor. If using an interactive shell which supports that extension (bash and some other implementations do but not all and it is not standard), you can also do it like this:

cmd 2>&1 >&- | grep pattern

This may be better - it can save some time, because when the command tries to write to standard output the call to write may fail immediately without waiting for a context switch into the kernel and the driver handling /dev/null (depending on the system call implementation - some may catch this in the libc function, and some may also have special handling for /dev/null). If there is a lot of output that can be worthwhile, and it's faster to type.

This will mostly work because most programs do not care if they fail to write to standard output (who really checks the return value of printf?) and will not mind that standard output is closed. But some programs can bail out with a failure code if write fails - usually block processors, programs using some careful library for I/O or logging to stdandard output. So if it doesn't work remember that this is a likely cause and try /dev/null.

Solution 2:

Close STDOUT first:

1>&-, >&-

See here.