我经常看到这样的命令:
command1 |& command2
所以我们在这里做的是将 stderr 重定向到 stdin,以便它们都“成为”管道中下一个元素的 stdin。
或在 Bash Reference manual -> Pipelines 中有更好的描述:
A pipeline is a sequence of simple commands separated by one of the control operators ‘|’ or ‘|&’.
...
If
|&
is used, command1’s standard error, in addition to its standard output, is connected to command2’s standard input through the pipe; it is shorthand for2>&1 |
. This implicit redirection of the standard error to the standard output is performed after any redirections specified by the command.
但是,我想知道:这种语法是否适用于所有 Bash 版本?也就是说,它总是可以与 &1 |
互换吗?它也可以在任何其他 shell(ksh、csh...)中使用吗?
最佳答案
此功能仅适用于 Bash 4+:
|& (bash4)
dd. The parser now understands `|&' as a synonym for `2>&1 |', which redirects the standard
error for a command through a pipe.
另请注意,checkbashisms 脚本也会对其进行标记:
'\s\|\&' => q<pipelining is not POSIX>,
关于bash - 我什么时候可以在 Bash 中使用 |&?它可以在其他 shell 中使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33192747/