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

标签 bash shell

我经常看到这样的命令:

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/

相关文章:

bash - 在 systemd 引导期间访问标准输入

bash - 如何从单个控制台在 20 多个站点上运行测试?

shell - Rsync 仅创建符号链接(symbolic link)

bash - 如何在 find -exec 语句中使用 >> ?

git - 是否可以接受用户输入作为远程 git post-receive Hook 的一部分?

php - 需要从日志中读取数据...tail、grep 和 inotifywait

linux - 邮件命令行为不当

linux - 使用 awk 和 bash 监视 exec 输出到日志

xcode - 如何从 xcode 构建阶段将参数传递给 shell 脚本

bash - 为 jq 数组中的每一项运行一个命令