【发布时间】:2014-07-28 18:07:59
【问题描述】:
根据 bash 中的process substitution,一个命令的标准输出可以使用以下模板一次通过管道传输到多个程序中:
echo 'foobar' | tee >(command1) >(command2) | command3
所以,你可以这样做:
echo "the fox jumped over the lazy dog" | tee >(grep fox) >(grep jumped)
并获得所有三个命令的输出。
现在我尝试存储所有这些命令的输出,但没有成功:
echo "the fox jumped over the lazy dog" | tee >(n1=$(grep fox)) >(n2=$(grep jumped))
echo $n1, $n2
您会看到$n1 和$n2 是空的!为什么?有没有办法让这个工作?
谢谢。
【问题讨论】:
-
由于您是
grep-ing 一行,因此 n1 和 n2 将具有相同的值。这是故意的吗? -
解决这个问题的方法是:stackoverflow.com/q/12451278/3596168
标签: bash pipe process-substitution