
For a long time, no one could explain to me what kind of ampersands, the <and> signs and numbers go after the unix commands. In this case, all the 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> (“more than”) operator, 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, conversational 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 file “STDERR” (error). All of these are three file descriptors (you can think of them as “data streams,” coming from the C programming language), often called STDIN, STDOUT, and STDERR.
But often they are called not by name, but by number:
0 - STDIN, 1 - STDOUT and 2 - STDERR
By default, if you do not specify a number, then STDOUT will be implied.
In our example, we can see that the command sends its standard output to / dev / null (a pseudo-device that can accept an arbitrary amount of data without saving it anywhere, hence suppressing the standard output). Then redirect all errors (i.e. STDERR) to standard output. It is necessary to put the ampersand "&" in front of the destination number.
Meaning in brief - "
shove the entire output of the specified command into a black hole! ".
This is one of the ways to make the program truly silent. I will add that the command in the example is similar to the command
cron job command >/dev/null 2>/dev/null
The official FreeBSD
FAQ warns: sending data to / dev / null / overheats your processor :)