这个 bash 命令 1>&2 中的和号表示什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2341023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:43:46  来源:igfitidea点击:

What does the ampersand indicate in this bash command 1>&2

bashscripting

提问by mikip

Quick one, 2>&1redirects stderr to stdout, but what does the ampersand mean? I know if we had 2 > 1it would output to a file named 1, what does the ampersand do?

快速一个,2>&1将 stderr 重定向到 stdout,但是 & 号是什么意思?我知道如果我们有2 > 1它会输出到一个名为 的文件1,与号有什么作用?

采纳答案by Ignacio Vazquez-Abrams

It copies file descriptor 1 to file descriptor 2. FD2 is stderr and FD1 is stdout, so it makes any output to stderr go to stdout instead.

它将文件描述符 1 复制到文件描述符 2。FD2 是标准错误,而 FD1 是标准输出,因此它使标准错误的任何输出改为标准输出。

回答by paxdiablo

2>&1redirects standard error (file handle 2) to the same file that standard output (file handle 1) is currentlygoing to.

2>&1将标准错误(文件句柄 2)重定向到标准输出(文件句柄 1)当前要去的同一个文件。

It's also a position-dependent thing so:

这也是一个依赖位置的事情,所以:

prog >x 2>&1 >y

will actually send standard error to xand standard output to yas follows:

实际上将标准错误发送到x标准输出,y如下所示:

  • Connect standard output to x;
  • Thenconnect standard error to same as current standard output, which is x;
  • Thenconnect standard output to y;
  • 将标准输出连接到x;
  • 然后将标准错误连接到与当前标准输出相同的位置,即x;
  • 然后将标准输出连接到y;

回答by Rose Perrone

The &in &1duplicates the file descriptor 1. The duplicated descriptor actually does not behave like a copy, but like an alias of the old one. Duplicating 1allows multiple streams to be redirected to 1without overwriting each other.

&&1复制的文件描述符1。重复的描述符实际上不像副本,而是旧描述符的别名。复制1允许将多个流重定向到1而不相互覆盖。

Example: (no &)

例子:(没有&

$ ls existing-file non-existent-file > tmp 2> tmp 
$ cat tmp
existing-file
nt-file: No such file or directory

Note that 1overwrote what 2wrote. But not when we use &:

请注意,1覆盖了2所写的内容。但不是当我们使用&

$ ls existing-file non-existent-file > tmp 2>&1 
$ cat tmp
ls: non-existent-file: No such file or directory
existing-file

A file descriptor is a handle to a file (or other input/output resource, such as a pipe or network socket). When 1and 2are separately redirected to tmp(as in the first example), they move their tmpfile pointer independently. That's why the file descriptors overwrote each other.

文件描述符是文件(或其他输入/输出资源,例如管道或网络套接字)的句柄。当12分别重定向到tmp(如第一个示例中)时,它们tmp独立地移动它们的文件指针。这就是文件描述符相互覆盖的原因。

According to the Linux man page:

根据Linux 手册页

[Duplicate file descriptors] refer to the same open file description and thus share file offset and file status flags; for example, if the file offset is modified by using lseek(2) on one of the descriptors, the offset is also changed for the other.

[重复文件描述符] 引用相同的打开文件描述,从而共享文件偏移量和文件状态标志;例如,如果在其中一个描述符上使用 lseek(2) 修改了文件偏移量,则另一个描述符的偏移量也会更改。

Note that even though &acts like an alias, 2>&1means redirect 2to the stream that 1in currentlypointing to. When 1is redirected to something else, 2points to the same file it did independently of 1.

需要注意的是,即使&像一个别名行为,2>&1手段重定向2到该流1目前指向。当1被重定向到其他东西时,2指向它独立于1.

Observe:

观察:

$ ls existing-file non-existent-file > tmp 2>&1 > tmp1
$ cat tmp1
existing-file
$ cat tmp
ls: non-existent-file: No such file or directory

回答by sleske

The ampersand belongs to the "1", so the snippet really has three parts: "2", ">", "&1". They mean, respectively, "take the data from output stream 2 (which is standard error)", "redirect it", and the redirection target, which is output stream 1. So "&" here allows you to redirect to an existing stream, rather than to a file.

&符号属于“1”,因此该代码段实际上包含三个部分:“2”、“>”、“&1”。它们的意思分别是“从输出流 2(这是标准错误)中获取数据”、“重定向它”和重定向目标,即输出流 1。所以这里的“&”允许您重定向到现有流, 而不是文件。

回答by Douglas Leeder

From info bash:

来自info bash

3.6.7 Duplicating File Descriptors
----------------------------------

The redirection operator
     [N]<&WORD
   is used to duplicate input file descriptors.  If WORD expands to one
or more digits, the file descriptor denoted by N is made to be a copy
of that file descriptor.  If the digits in WORD do not specify a file
descriptor open for input, a redirection error occurs.  If WORD
evaluates to `-', file descriptor N is closed.  If N is not specified,
the standard input (file descriptor 0) is used.

   The operator
     [N]>&WORD
   is used similarly to duplicate output file descriptors.  If N is not
specified, the standard output (file descriptor 1) is used.  If the
digits in WORD do not specify a file descriptor open for output, a
redirection error occurs.  As a special case, if N is omitted, and WORD
does not expand to one or more digits, the standard output and standard
error are redirected as described previously.

So 2>&1duplicates fd 1 onto fd 2.

所以2>&1将 fd 1 复制到 fd 2 上。

回答by Pete Kirkham

The ampersanddoesn't do anything - it's the character in the 2>&1operator rather than being a thing in its own right.

符号并不做任何事情-它在字符2>&1操作,而不是在自己的权利的事情。

bash supports several redirection operators, the 2>&1operator or the &>operator tie together the streams of the process before or after redirection.

bash 支持多个重定向操作符2>&1操作符或&>操作符将重定向前后的流程流联系在一起。