注:本文为 “Process Substitution” 相关文章合辑。
英文引文机翻,未校。
Process Substitution.
进程替换允许使用文件名引用进程的输入或输出。它采取以下形式
<(list)
or
>(list)
进程 list 异步运行,其输入或输出显示为文件名。该文件名作为扩展结果作为参数传递给当前命令。如果使用 >(list) 形式,写入文件将为 list 提供输入。如果使用 <(list) 形式,则应读取作为参数传递的文件以获得 list 的输出。请注意,< 或 > 与左括号之间不得出现空格,否则该结构将被解释为重定向。支持命名管道 (FIFO) 或/dev/fd 命名打开文件的方法。
如果可用,进程替换与参数和变量扩展、命令替换和算术扩展同时执行。
Bash process substitution Bash 进程替换
Posted on 2012-02-27 by Tom Ryder
For tools like diff
that work with multiple files as parameters, it can be useful to work with not just files on the filesystem, but also potentially with the output of arbitrary commands. Say, for example, you wanted to compare the output of ps
and ps -e
with diff -u
. An obvious way to do this is to write files to compare the output:
对于像 diff
这样使用多个文件作为参数的工具,不仅使用文件系统上的文件,而且可能使用任意命令的输出也很有用。例如,假设您想将 ps
和 ps -e
的输出与 diff -u
进行比较。一个明显的方法是编写文件来比较输出:
$ ps > ps.out
$ ps -e > pse.out
$ diff -u ps.out pse.out
This works just fine, but Bash provides a shortcut in the form of process substitution, allowing you to treat the standard output of commands as files. This is done with the <()
and >()
operators. In our case, we want to direct the standard output of two commands into place as files:
这工作得很好,但 Bash 以进程替换的形式提供了一个快捷方式,允许您将命令的标准输出视为文件。这是通过 <()
和 >()
运算符完成的。在我们的例子中,我们希望将两个命令的标准输出作为文件定向到位:
$ diff -u <(ps) <(ps -e)
This is functionally equivalent, except it’s a little tidier because it doesn’t leave files lying around. This is also very handy for elegantly comparing files across servers, using ssh
:
这在功能上是等效的,只是它更整洁一些,因为它不会留下文件。这对于使用 ssh
优雅地比较不同服务器的文件也非常方便