Bash 中的“> (tee -a ...)”命令

The "> (tee -a ...)" command in Bash

我有这个 Bash 运行 Scala 测试代码的代码:

scripts=(
Hello.scala
)

for script in "${scripts[@]}"; do
    echo scala "${script}"
    scala -nocompdaemon "${script}" > >(tee -a _testoutput.txt) \
        2> >(tee -a _testerrors.txt >&2)
done

如何解释 >(tee -a _testoutput.txt)?我通常使用 |(管道)来使用 tee。使用这个表达式有什么区别?

在这种情况下,我相信 > >(tee -a _testoutput.txt)| tee -a _testoutput.txt 的行为相同。

显然需要 standard error 版本,因为没有标准错误管道。

管道版本和 process substitution (>(...)) 版本之间的另一个主要区别是子外壳发生的地方。

例如,如果 >(...) 在整个循环中,并且您需要在循环中设置的变量在循环之外持续存在,则管道版本无法做到这一点(请参阅 Bash FAQ 24了解更多信息)。

一个额外的区别,正确地 ,是管道影响管道的退出状态(默认情况下,您获得管道中最终命令的退出状态,尽管 set -o pipefail 改变了Bash PIPESTATUS 数组包含所有退出状态)。另一方面,进程替换不会影响退出状态。

>( list ) 被称为 "process substitution"。它比普通管道更强大:您不能使用 | 如此轻易地将标准输出和标准错误重定向到不同的程序。