如何在 Linux 命令中两次引用上一个命令的输出?

How to reference the output of the previous command twice in Linux command?

例如,如果我想引用上一个命令一次的输出,我可以使用下面的命令:

ls *.txt | xargs -I % ls -l %

但是如何引用两次输出?就像我怎样才能实现这样的东西:

ls *.txt | xargs -I % 'some command' % > %

PS: 我知道如何在 shell 脚本中做到这一点,但我只是想要一种更简单的方法来做到这一点。

您可以将此参数传递给 bash -c:

ls *.txt | xargs -I % bash -c 'ls -l "" > "out."' - %

您可以在 SO 上查找 'tpipe';它还会将您带到 'pee'(这在 Internet 的其他地方不是一个好的搜索词)。基本上,它们是 tee 命令的变体,它写入多个进程而不是像 tee 命令那样写入文件。

但是,对于 Bash,您可以使用 Process Substitution:

ls *.txt | tee >(cmd1) >(cmd2)

这会将 tee 的输入写入每个命令 cmd1cmd2

您至少可以通过两种不同的方式安排丢失标准输出:

ls *.txt | tee >(cmd1) >(cmd2) >/dev/null
ls *.txt | tee >(cmd1) | cmd2