Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a program whose STDERR output I want to inspect and run grep on etc.

So I could redirect it to STDOUT and use grep, but the problem is, I do not want the original STDOUT content.

So, this one won't do

cmd 2>&1 | grep pattern 

because it will mix the original STDOUT and STDERR.

And this one doesn't work since grep doesn't read the STDERR output:

cmd 1>/dev/null | grep pattern 

But also, this one won't work:

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

because the output will be completely empty, since everything gets written to /dev/null.

But there must be a simple way to do it?

like image 709
Frank Avatar asked Feb 14 '09 20:02

Frank


People also ask

How do I redirect stderr and stdout?

The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.

How can I redirect stdout and stderr in same location?

Redirecting stdout and stderr to a file: The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator.

How do I redirect both standard and standard error on the same location?

Discussion. &> or >& is a shortcut that simply sends both STDOUT and STDERR to the same place—exactly what we want to do.


1 Answers

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.

like image 151
Tom Alsberg Avatar answered Sep 29 '22 01:09

Tom Alsberg