Linux Shell script programming -- (Linux empty device file and redirection)>/dev/null 2> & 1

Source: Internet
Author: User

Linux empty device file and redirection

Output/input redirection
>>><<:>&> 2 &> 2 <>>&> & 2

File descriptor (file descriptor), which is represented by a number (usually 0-9.
Common file descriptors are as follows:
Default file descriptor name
0 standard input stdin keyboard
1. stdout screen output
2. stderr screen output due to standard errors

 

/Dev/null indicates an empty file device.
1 indicates the system standard output. The default value is 1.>/dev/null is equivalent to 1>/dev/null.

For> DEV/null 2> & 1
1>/dev/null: indicates that the standard output is redirected to an empty device file, that is, no information is output to the terminal. In other words, no information is displayed.
2> & 1: The standard error output is redirected to the standard output. Because the standard output has been redirected to the empty device file, the standard error output is also redirected to the empty device file.

Comand> file 2> file and Comand> file 2> & 1
Command> file 2> file refers to sending the standard output information and error output information generated by the command to file. command> file 2> file: Both stdout and stderr are directly sent to file. The file will be opened twice, so that stdout and stderr will overwrite each other, in this way, both fd1 and fd2 are used to seize the file pipeline at the same time.

Command> file 2> & 1 directly sends stdout to file. After stderr inherits the fd1 pipeline, it is sent to file. At this time, the file is opened only once, only one pipeline fd1 is used, which includes stdout and stderr content.

In terms of Io efficiency, the efficiency of the previous command is lower than that of the subsequent command. Therefore, when writing shell scripts, in many cases, we will write commands> file 2> & 1.

In the Linux Shell environment, input and output redirection is supported, expressed by symbols <and>. 0, 1, and 2 indicate the standard input, standard output, and standard error information output, which can be used to specify the standard input or output to be redirected. For example, 2> a.txt outputs the error information to file a.txt.

At the same time, you can also implement redirection between the three standard input and output. For example, you can use 2> & 1 to redirect the error message to the standard output.

In Linux, there is also a special file/dev/null, which is like a bottomless pit, and all the information redirected to it will disappear without a trace. This is very useful. When we do not need to display all information about the program, we can redirect the output to/dev/null.

To disable normal output and error information, redirect both standard output and standard errors to/dev/null. For example:

# Ls 1>/dev/null 2>/dev/null

Another way is to redirect errors to standard output and then to/dev/null, for example:

# Ls>/dev/null 2> & 1

Note: The order here cannot be changed. Otherwise, the desired effect cannot be reached. In this case, the standard output is redirected to/dev/null and the standard error is redirected to the standard output, because the standard output has been redirected to/dev/null, the standard error will also be redirected to/dev/null,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.