gpt4 book ai didi

bash - 我什么时候可以在 Bash 中使用 |&?它可以在其他 shell 中使用吗?

转载 作者:行者123 更新时间:2023-11-29 09:00:40 24 4
gpt4 key购买 nike

我经常看到这样的命令:

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 for 2>&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)

Source


dd. The parser now understands `|&' as a synonym for `2>&1 |', which redirects the standard
error for a command through a pipe.

Source


另请注意,checkbashisms 脚本也会对其进行标记:

'\s\|\&' =>                    q<pipelining is not POSIX>,

Source

关于bash - 我什么时候可以在 Bash 中使用 |&?它可以在其他 shell 中使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33192747/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com