如何为不同的 shell 组合 stdout 和 stderr?

pes*_*che 18 bash shell command-line

我知道一些 shell 的答案,例如cmd.exe你可以:

some.exe > out.txt 2>&1
Run Code Online (Sandbox Code Playgroud)

您如何为其他 shell(bash、ksh、tcsh、powershell 等)实现相同的目标?

Gri*_*ave 16

鱼壳

要将标准输出和标准错误重定向到文件 all_output.txt,您可以编写:

echo Hello > all_output.txt ^&1
Run Code Online (Sandbox Code Playgroud)

  • 如果管道输出到下一个命令,请使用 2>| 相反:回声你好2>| 较少的 (3认同)

Kei*_*thB 5

对于 csh 和 tcsh

some.exec >& out.txt
Run Code Online (Sandbox Code Playgroud)


Joe*_*oey 3

Powershell中它是完全相同的:

2>&1 向 get-process 发送错误 none, powershell 2>&1
          成功输出流。

(从about_Redirection)。

bash和 ksh 中至少我也知道它是这样工作的。

这似乎是一个共同的约定。

一种快速了解它的方法是打开 shell 的手册页并搜索&1(使用/,然后键入&1)。这在其他情况下很少发生。