2020年7月11日星期六

Bash process substitution, named pipe, or < <()

If you at some point run into a bash code block, while maybe, and at its end you see something like

< <(some_command)

You might then ask, what the heck is this and what does it do?

The <(some_command) part is called process substitution. In a nutshell it serves as a file name, whose content is substituted by the execution output of some_command. The first < then feeds the content to the code block as stdin, the standard IO redirection as you are already familiar with.

Can I just do some_command | while ... which seems more straightforward? Well, sometimes you can. But you should know that in this way the while part is executed in a sub shell and whatever variable you modify there won't be persisted in the current shell.

Under the hood process substitution is implemented by named pipes or /dev/fd.

References:
https://www.linuxjournal.com/content/shell-process-redirection
https://www.linuxjournal.com/article/2156

没有评论:

发表评论