Get the App
SLTechnology News&Howtos  ›  Servers  › 

What is the meaning of 1 roomgtanspact 1 in shell

Shulou Source: shulou.com Published: 2022-05-31 15:17:12 09月16日 Update

This article is to share with you about the meaning of 1 > / dev/null 2 > & 1 in shell. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Without saying much, let's take a look at it.

You can often see in shell that the result of > / dev/null 2 > & 1 command can be defined in the form of% > output / dev/null represents empty device file > where to redirect. For example, echo "123" > / home/123.txt 1 represents stdout standard output, and the system default value is 1, so "> / dev/null" equals "1 > / dev/null" 2 to mean stderr standard error. 2 > & 1, which means that the output redirection of 2 is equivalent to 1. Then the statement in the title of this article: 1 > / dev/null first indicates that the standard output is redirected to the empty device file, that is, no information is output to the terminal. To put it bluntly, no information is displayed. 2 > & 1 then, the standard error output redirection is equivalent to the standard output, because the standard output has been redirected to the empty device file, so the standard error output is also redirected to the empty device file.

Why use / dev/null 2 > & 1 to write. This command means that all standard output and error output are redirected to / dev/null, that is, all generated information is discarded. Let me tell you what's the difference between command > file 2 > file and command > file 2 > & 1.

First of all, ~ command > file 2 > file means to send the standard output information and error output information generated by the command to file. In the way of writing command > file 2 > file, both stdout and stderr are sent directly to file, and file will be opened twice, so that stdout and stderr will overwrite each other, which is equivalent to using both FD1 and FD2 to seize file pipes at the same time.

The command command > file 2 > & 1 sends stdout directly to file. After stderr inherits the FD1 pipeline, it is sent to file. At this time, file is opened only once, and only one pipeline FD1 is used, which includes the contents of stdout and stderr.

In terms of IO efficiency, the former command is less efficient than the latter command, so when writing shell scripts, we often use command > file 2 > & 1.

The above is the meaning of 1 > / dev/null 2 > & 1 in shell. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Output standard command information error file vacancy writing meaning efficiency pipeline meaning that is representative time more knowledge article different Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS vpn MySQL Linux Shulou Tech Info