Q:
To merge stderr and stdout into a stdout stream, we append it to the command:
For example, to see the compilation of g++ mainThe first few errors of CPP:
g++ main.cpp 2>&1 | head
What exactly does 2>&1 mean?
A:
File descriptor 1 is stdout.
File descriptor 2 is a standard error (stderr).
At first, 2>1 may seem like a great way to redirect stderr to stdout. However, it will actually be interpreted as "redirecting stderr to a file with the name 1".
Indicates that the (numbers) that follow and precede it are file descriptors, not file names. Therefore, we use 2>&1. Consider >& as the redirect merge operator.
Related knowledge points:
In operating system terminology, a running program is referred to as a process. Each process in a UNIX Linux Posix (including Windows) environment has three different input/output file descriptors: Standard Input (STDIN), Standard Output (STDOUT), and Standard Error (STDERR).
stdin is the program's default input source – by default, the characters come from the keyboard. Corresponding file descriptor number 0.
stdout is the default location for the sender's output, which by default appears in a window where the shell or shell script is run.
The stderr standard error can also be sent from within the program to the output, but it should only be output to the place where the error message is written in the program.
The shell provides a mechanism for you to have a running program change its input and output positions without modifying the program itself, and this technique is redirection.
Reference: stackoverflow question 818255
Bash Cyber Security Operations and Operations