Thursday, 31 August 2017

bash - How to pipe stderr, and not stdout?




I have a program 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 this without temp files. Are there any smart piping tricks?


Answer




First redirect stderr to stdout — the pipe; then redirect stdout to /dev/null (without changing where stderr is going):



command 2>&1 >/dev/null | grep 'something'


For the details of I/O redirection in all its variety, see the chapter on Redirections in the Bash reference manual.



Note that the sequence of I/O redirections is interpreted left-to-right, but pipes are set up before the I/O redirections are interpreted. File descriptors such as 1 and 2 are references to open file descriptions. The operation 2>&1 makes file descriptor 2 aka stderr refer to the same open file description as file descriptor 1 aka stdout is currently referring to (see dup2() and open()). The operation >/dev/null then changes file descriptor 1 so that it refers to an open file description for /dev/null, but that doesn't change the fact that file descriptor 2 refers to the open file description which file descriptor 1 was originally pointing to — namely, the pipe.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...