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 does 2 > & 1 mean in linux shell

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the meaning of 2 > & 1 in linux shell. It has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to understand it.

The meaning of "2 > & 1" in linux shell

Script:

Nohup / mnt/Nand3/H2000G > / dev/null 2 > & 1 &

For & 1, more accurately, it should be the file descriptor 1, and 1 generally represents STDOUT_FILENO, which is actually a dup2 (2) call. He standard output to all_result, and then copy standard output to file descriptor 2 (STDERR_FILENO). As a result, file descriptors 1 and 2 point to the same file table entry, or the wrong output is merged. Where 0 for keyboard input 1 for screen output 2 for error output. Redirect standard errors to standard output and throw them under / DEV/NULL. In popular terms, it is to throw all standard output and standard errors into the dustbin.

Command > out.file 2 > & 1 &

Command > out.file redirects the output of command to the out.file file, that is, the output is not printed to the screen, but to the out.file file. 2 > & 1 redirects the standard error to the standard output, where the standard output has been redirected to the out.file file, that is, the standard error is also output to the out.file file. The last & is to let the command be executed in the background.

Imagine what 2 > 1 stands for, 2 and > represent error redirection, and 1 represents error redirection to a file 1, not standard output

Replace it with 2 > & 1 & combining with 1 represents standard output and becomes an error redirection to standard output.

Can be used

Ls 2 > 1 test will not report an error without 2 files, but will output an empty file 1

Ls xxx 2 > 1 test, no xxx this file error output to 1

Ls xxx 2 > & 1 test, will not generate 1 this file, but the error went to standard output

Ls xxx > out.txt 2 > & 1, which can actually be replaced with ls xxx 1 > out.txt 2 > & 1; the redirect symbol > defaults to 1, and the error and output are sent to out.txt.

What 2 > & 1 should be written at the end?

Command > file 2 > & 1

First, command > file redirects standard output to file. 2 > & 1 is the act of standard error copying standard output, that is, it is also redirected to file, and the end result is that both standard output and error are redirected to file.

Command 2 > & 1 > file

2 > & 1 standard error copies the behavior of standard output, but at this time the standard output is still on the terminal. The output is redirected to file after file, but the standard error remains on the terminal.

With strace, you can see:

1. Command > file 2 > & 1

The key system call sequence to achieve redirection in this command is:

Open (file) = = 3

Dup2 (3Phone1)

Dup2 (1 dint 2)

2. Command 2 > & 1 > file

The key system call sequence to achieve redirection in this command is:

Dup2 (1 dint 2)

Open (file) = = 3

Dup2 (3Phone1)

Thank you for reading this article carefully. I hope the article "what is the meaning of 2 > & 1 in linux shell" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Development

Wechat

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

12
Report