Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the meaning of 1 roomgtanspact 1 in shell

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report