advertisement

2021年9月14日

What is /dev/null 2>&1 linux file descriptor 0 1 2 for stdin, stdout and stderr In the shell, what does " 2>&1 " mean?

 

         The input stream is referred to as "standard input"; the output
       stream is referred to as "standard output"; and the error stream
       is referred to as "standard error".  These terms are abbreviated
       to form the symbols used to refer to these files, namely stdin,
       stdout, and stderr.
         On program startup, the integer file descriptors associated with
       the streams stdin, stdout, and stderr are 0, 1, and 2,
       respectively.  The preprocessor symbols STDIN_FILENO,
       STDOUT_FILENO, and STDERR_FILENO are defined with these values in
       <unistd.h>.


There are also symbolic constants defined in unistd.h for the file descriptors belonging to the standard streams stdinstdout, and stderr; see Standard Streams.

STDIN_FILENO

This macro has value 0, which is the file descriptor for standard input.

STDOUT_FILENO

This macro has value 1, which is the file descriptor for standard output.

STDERR_FILENO

This macro has value 2, which is the file descriptor for standard error output.


File descriptor 1 is the standard output (stdout).
File descriptor 2 is the standard error (stderr).

Here is one way to remember this construct (although it is not entirely accurate): at first, 2>1 may look like a good way to redirect stderr to stdout. However, it will actually be interpreted as "redirect stderr to a file named 1". & indicates that what follows and precedes is a file descriptor and not a filename. So the construct becomes: 2>&1.

Consider >& as redirect merger operator.


Ref:

https://www.gnu.org/software/libc/manual/html_node/Descriptors-and-Streams.html

https://man7.org/linux/man-pages/man3/stdout.3.html

https://stackoverflow.com/questions/818255/in-the-shell-what-does-21-mean?noredirect=1&lq=1

Double pipe Double logical and Control operators

 

expression )

Returns the value of expression. This may be used to override the normal precedence of operators.

expression

True if expression is false.

expression1 && expression2

True if both expression1 and expression2 are true.

expression1 || expression2

True if either expression1 or expression2 is true.

The && and || operators do not evaluate expression2 if the value of expression1 is sufficient to determine the return value of the entire conditional expression.


For example:

Code:
$ ls this_file_does_not_exist.txt || echo KO
ls: cannot access this_file_does_not_exist.txt: No such file or directory
KO

$ ls this_file_exist.txt && echo OK
this_file_exist.txt
OK


Ref:

https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Command-Grouping

https://pongsona.wordpress.com/2011/12/03/what-does-double-pipe-mean/


What does @: (at symbol colon) mean in a Makefile?

 https://stackoverflow.com/questions/8610799/what-does-at-symbol-colon-mean-in-a-makefile

https://unix.stackexchange.com/questions/192241/file-function-in-makefile-takes-args-prefixed-by-symbol