将std错误重定向到文件时的顺序为2>&1

时间:2022-09-03 00:05:36

I'm seeing some different formats for redirecting std output to a file :

我看到一些不同的格式将std输出重定向到文件:

a.  command 1&2>output.txt
b.  command >output.txt 2>&1
c.  command 2>&1>output.txt  
d.  command &>output.txt

Is there any difference between these? If 2>&1 is placed at the end (b) , how does it redirect the stderr of the first command ?

这些之间有什么区别吗?如果2>&1放在末尾(b),它如何重定向第一个命令的stderr?

1 个解决方案

#1


Yes. The order matters. > may bring up the idea of pointing and pointers/references and so does the word "redirect", but fd redirections are more like assignments. That is, if you do

是。订单很重要。 >可能会提出指向和指针/引用的想法,“redirect”这个词也是如此,但fd重定向更像是赋值。也就是说,如果你这样做

exec 2>&1 1>output.txt

exec 2>&1 1> output.txt

It will "assign" the current "value" (the actual file opened with that file descriptor) of file descriptor 1 to file descriptor 2 and then open output.txt and assign it to file descriptor 1.

它将文件描述符1的当前“值”(用该文件描述符打开的实际文件)“分配”到文件描述符2,然后打开output.txt并将其分配给文件描述符1。

What it won't do is point &2 (read & as "file descriptor") to &1. It won't make accessing &2 query &1. A file descriptor is only ever associated with an actual file, never with another file descriptor. 2>&1 associates the file now opened under &1 with &2. It doesn't redirect &2 to &1 in the sense that writing to &2 would make it write to what &1 is associated with at the moment. &1 can later be reopened with a different file that what it was associated with at the time of the 2>&1 redirection, but that won't affect what &2 writes to.

它不会做的是将&2(读作“文件描述符”)指向&1。它不会进行访问&2查询&1。文件描述符只与实际文件相关联,从不与另一个文件描述符相关联。 2>&1将现在在&1下打开的文件与&2相关联。它不会重定向&2到&1,因为写入&2会使它写入与1相关联的内容。 &1以后可以使用与2>&1重定向时相关联的不同文件重新打开,但这不会影响&2写入的内容。

Check out dup2(2) if you want to know how this functionality is exposed at the system call level.

如果您想知道在系统调用级别如何公开此功能,请查看dup2(2)。

#1


Yes. The order matters. > may bring up the idea of pointing and pointers/references and so does the word "redirect", but fd redirections are more like assignments. That is, if you do

是。订单很重要。 >可能会提出指向和指针/引用的想法,“redirect”这个词也是如此,但fd重定向更像是赋值。也就是说,如果你这样做

exec 2>&1 1>output.txt

exec 2>&1 1> output.txt

It will "assign" the current "value" (the actual file opened with that file descriptor) of file descriptor 1 to file descriptor 2 and then open output.txt and assign it to file descriptor 1.

它将文件描述符1的当前“值”(用该文件描述符打开的实际文件)“分配”到文件描述符2,然后打开output.txt并将其分配给文件描述符1。

What it won't do is point &2 (read & as "file descriptor") to &1. It won't make accessing &2 query &1. A file descriptor is only ever associated with an actual file, never with another file descriptor. 2>&1 associates the file now opened under &1 with &2. It doesn't redirect &2 to &1 in the sense that writing to &2 would make it write to what &1 is associated with at the moment. &1 can later be reopened with a different file that what it was associated with at the time of the 2>&1 redirection, but that won't affect what &2 writes to.

它不会做的是将&2(读作“文件描述符”)指向&1。它不会进行访问&2查询&1。文件描述符只与实际文件相关联,从不与另一个文件描述符相关联。 2>&1将现在在&1下打开的文件与&2相关联。它不会重定向&2到&1,因为写入&2会使它写入与1相关联的内容。 &1以后可以使用与2>&1重定向时相关联的不同文件重新打开,但这不会影响&2写入的内容。

Check out dup2(2) if you want to know how this functionality is exposed at the system call level.

如果您想知道在系统调用级别如何公开此功能,请查看dup2(2)。

相关文章