What does "> / dev / null 2> & 1" mean?

Original author: Xaprb
  • Transfer
For a long time no one could explain to me what ampersands, signs <and> and numbers come after Unix commands. Moreover, all examples were shown without explanation - why all this? Google also did not give an answer. Especially noticeable is the use of such commands while the compiler is running. In this article I will try to explain these strange commands.

For example, we have this line:
cron job command > /dev/null 2>&1



Output redirection

The> operator ("more than"), as in the example above, redirects the output of the program. In this case, something is sent to / dev / null, and something is redirected to & 1.

Standard input, output and error

There are three standard input and output values ​​for programs. Input is received from the keyboard (interactive, interactive program), or from a program that processes the output of another program.
The result of the program is usually printed in standard output and sometimes in the STDERR file (error). These are all three file descriptors (you can think of them as “data streams”, come from the C programming language), which are often called STDIN, STDOUT and STDERR.

But often they are accessed not by name, but by number:
0 - STDIN, 1 - STDOUT and 2 - STDERR
By default, if you do not specify a number, it will mean STDOUT.

In our example, we see that the command directs its standard output to / dev / null (a pseudo-device that can take an arbitrary amount of data without saving it completely anywhere, therefore, suppressing standard output). Then redirect all errors (i.e. STDERR) to standard output. You must prefix the ampersand "&" with the destination number.

The meaning in short is "the entire output of the indicated command is shoved into a black hole! ".
This is one way to make the program truly silent. I will add that the command in the example is similar to the cron job command >/dev/null 2>/dev/null

official FreeBSD Official FAQ warns: sending data to / dev / null / overheats your processor :)

Also popular now: