Programs are sending message on two different steam of data: standard output and
standard error. Standard output, notes as stdout
, is where the program
is writing the output data. Standard error or stderr
is to output the
error messages or diagnostics. With this two stream we can therefore separate
between these two kind of output and process them separately. A third stream of
data exist, that is standard input or stdin, which correspond to the entry of
data for a program.
Classical shell programming involved to play with these different stream of
data, where the stdout
of one program is pipe to the stdin
of an
other program to get the desired functionality. One of this task is to filter
the output of a command with grep
.
$ ls -1
Desktop
Documents
Pictures
Videos
Download
Music
$ ls -1 | grep D
Desktop
Documents
Download
However, pipe is only for redirection of stdout
to the stdin
.
Sometime we would like to filter the stderr
and not the sdtout
.
Therefore we need to redirect the stream so they can be processed correcrtly.
First we can redirect stderr
to stdout
and mix the two output
that will be then filtered:
$ myprog 2>&1 | grep something
In the same time we can destroy the output of stdout
and only keep
stderr
to be filtered:
$ myprog 2>&1 > /dev/null | grep something
You have to be carefull in the order of the command since the following do not seems to work as expected, both stream being redirected to /dev/null
$ myprog > /dev/null 2>&1| grep something