cmd 2>和1 >和cmd > lb3和1

时间:2021-01-15 18:06:01

What is the difference between:

的区别是什么:

cmd > log 2>&1

and

cmd 2>&1 > log

where cmd is a command?

cmd是命令吗?

Which should I prefer and why?

我应该选择哪一个,为什么?

2 个解决方案

#1


75  

Order matters. The way to reason about redirections is to read them from left to right and realize that redirections make streams point at the same place. They don't make streams point at each other.

顺序很重要。解释重定向的方法是从左到右读取它们,并意识到重定向使流指向相同的位置。它们不会让流指向彼此。

What does that mean? If you say 2>&1 then you are redirecting stderr to wherever stdout is currently redirected to. If stdout is going to the console then stderr is, too. If stdout is going to a file then stderr is as well. If you follow this up by then redirecting stdout, stderr still points to what stdout used to point to. It does not "follow" stdout to the new location.

这是什么意思?如果您说2>和1,那么您正在将stderr重定向到stdout当前重定向到的任何地方。如果stdout是到控制台的,那么stderr也是。如果stdout指向一个文件,那么stderr也是。如果你按照这个方法来执行stdout, stderr仍然指向stdout的指向。它不会“跟踪”stdout到新的位置。

Right

cmd > log 2>&1

This redirects stdout to log and then redirects stderr to wherever stdout is now being redirected, which is log.

它将stdout重定向到日志,然后将stderr重定向到任何stdout现在被重定向的地方,即日志。

End result: both stdout and stderr are redirected to log.

最终结果:stdout和stderr都被重定向到log。

Wrong

cmd 2>&1 > log

This redirects stderr to wherever stdout is currently being redirected, which is typically the console. Then stdout is redirected to log. Remember that stderr does not "follow" stdout, so it continues to redirect to the console.

这将把stderr重定向到当前正在重定向的任何地方,这通常是控制台。然后将stdout重定向到日志。记住,stderr不“跟随”stdout,因此它将继续重定向到控制台。

End result: stdout is redirected to the log file and stderr is (still) sent to the console. This is almost certainly not what you want.

最终结果:stdout被重定向到日志文件,stderr(仍然)被发送到控制台。这几乎肯定不是你想要的。

#2


6  

cmd > log 2>&1

Redirects STDOUT to log and than replaces STDERR with the redirected STDOUT.

将STDOUT重定向到日志,并用重定向的STDOUT替换STDERR。

cmd 2>&1 > log

Replaces STDERR with STDOUT and then redirects the original STDOUT to log.

用STDOUT替换STDERR,然后将原来的STDOUT重定向到日志。

#1


75  

Order matters. The way to reason about redirections is to read them from left to right and realize that redirections make streams point at the same place. They don't make streams point at each other.

顺序很重要。解释重定向的方法是从左到右读取它们,并意识到重定向使流指向相同的位置。它们不会让流指向彼此。

What does that mean? If you say 2>&1 then you are redirecting stderr to wherever stdout is currently redirected to. If stdout is going to the console then stderr is, too. If stdout is going to a file then stderr is as well. If you follow this up by then redirecting stdout, stderr still points to what stdout used to point to. It does not "follow" stdout to the new location.

这是什么意思?如果您说2>和1,那么您正在将stderr重定向到stdout当前重定向到的任何地方。如果stdout是到控制台的,那么stderr也是。如果stdout指向一个文件,那么stderr也是。如果你按照这个方法来执行stdout, stderr仍然指向stdout的指向。它不会“跟踪”stdout到新的位置。

Right

cmd > log 2>&1

This redirects stdout to log and then redirects stderr to wherever stdout is now being redirected, which is log.

它将stdout重定向到日志,然后将stderr重定向到任何stdout现在被重定向的地方,即日志。

End result: both stdout and stderr are redirected to log.

最终结果:stdout和stderr都被重定向到log。

Wrong

cmd 2>&1 > log

This redirects stderr to wherever stdout is currently being redirected, which is typically the console. Then stdout is redirected to log. Remember that stderr does not "follow" stdout, so it continues to redirect to the console.

这将把stderr重定向到当前正在重定向的任何地方,这通常是控制台。然后将stdout重定向到日志。记住,stderr不“跟随”stdout,因此它将继续重定向到控制台。

End result: stdout is redirected to the log file and stderr is (still) sent to the console. This is almost certainly not what you want.

最终结果:stdout被重定向到日志文件,stderr(仍然)被发送到控制台。这几乎肯定不是你想要的。

#2


6  

cmd > log 2>&1

Redirects STDOUT to log and than replaces STDERR with the redirected STDOUT.

将STDOUT重定向到日志,并用重定向的STDOUT替换STDERR。

cmd 2>&1 > log

Replaces STDERR with STDOUT and then redirects the original STDOUT to log.

用STDOUT替换STDERR,然后将原来的STDOUT重定向到日志。