<() 在 Bash 中有什么作用? [英] What does <() do in Bash?
问题描述
在 帖子中 在 superuser.com 上回答,我们看到了
In a post's answer on superuser.com, we see that
join <(sort abc) <(sort bcd)
在将文件 abc 和 bcd 发送到加入之前对其进行排序.这就引出了一个编程问题,更适合 stackoverflow.
will sort files abc and bcd before sending them to join. This leads to a programming question, better suited for stackoverflow.
这是如何工作的?这个 <() 结构到底是什么?它叫什么?
How does this work? What exactly is this <() construct? What's it called?
如果 (sort abc) 是在 abc 上运行 sort 并返回输出的合法调用,为什么我们需要 <
?
If (sort abc) is a legal call that runs sort on abc and returns output, why do we need the <
?
即下面两行是等价的
(sort abc) | join - <(sort bcd)
join <(sort abc) <(sort bcd)
但是
join (sort abc) (sort bcd)
是语法错误.请给我提示!
is a syntax error. Please clue me in!
推荐答案
这称为进程替换.
<(
list
)
是一个单一的语法结构,'<'在这种情况下,字符不是单独的符号.它执行 list 并将其输出作为一种文件(不是标准重定向)提供给命令.
<(
list
)
is a single syntax construct, the '<' character is not a separate symbol in this case. It executes list and provides its output as sort of a file (not a standard redirection) to the command.
相当于运行(除了尽可能使用管道而不是临时文件):
It is equivalent to running (except it uses pipes instead of temporary files when possible):
sort abc > /tmp/1
sort bcd > /tmp/2
join /tmp/1 /tmp/2
请注意,两种类型的输出都作为要加入的文件名提供,而不是作为标准重定向.
Note that the output of both sorts are provided as filenames to join, not as standard redirections.
(
list
)
是不同的构造,用于不同的目的.它只是创建一个执行 list 的子 shell,将其标准描述符提供给父 shell.
(
list
)
is a different construct, for a different purpose. It simply creates a subshell that executes list, providing its standard descriptors to the parent shell.
这里是bash中的相关部分手册.
Here is the relevant part in the bash manual.
这篇关于<() 在 Bash 中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!