From man bash
, for “process substitution” it says
It takes the form of <(list) or >(list)
Note: there is no space between <
or >
and (
.
Also, it says,
The process list is run with its input or output connected to a FIFO or some file in /dev/fd. The name of this file is passed as an argument to the current command as the result of the expansion.
The <(list)
Form
$ diff <(echo a) <(echo b)
1c1
< a
---
> b
Usually the diff
takes two files and compares them.
The process substitution here, <(echo a)
, creates a file in /dev/fd
, for example /dev/fd/63
.
The stdout of echo a
command is connected to /dev/fd/63
.
Meanwhile, /dev/fd/63
is used as an input file/parameter of diff
command.
Similar for <(echo b)
.
After Bash does the substitution, the command is like diff /dev/fd/63 /dev/fd/64
.
In diff
’s point of view, it just compares two normal files.
In this example, one advantage of process substitution is eliminating the need of temporary files, like
$ echo a > tmp.a && echo b > tmp.b \
&& diff tmp.a tmp.b \
&& rm tmp.{a,b}
The >(list)
Form
$ echo . | tee >(ls)
Similar, Bash creates a file in /dev/fd
when it sees >(ls)
.
Again, let’s say the file is /dev/fd/63
.
Bash connects /dev/fd/63
with stdin of ls
command, also the file /dev/fd/63
is used as a parameter of
tee
command.
The tee
views /dev/fd/63
as a normal file.
tee
writes content, here is .
, into the file, and the content will “pipe” into the stdin of ls
.
Compare with Pipe
Pipe, cmd-a | cmd-b
, basically just passes stdout of the command on the left to the stdin of the command on the right.
Its data flow is restricted, which is from left to right.
Process substitution has more freedom.
# use process substitution
$ grep -f <(echo hello) file-a
hello
# use pipe
$ echo hello | xargs -I{} grep {} file-a
hello
And for commands like diff <(echo a) <(echo b)
, it’s not easy to be done by pipe.
More Examples
$ paste <(echo hello) <(echo world)
hello world
How it works
From man bash
,
Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files.
Read more about named pipe and /dev/fd.
For /dev/fd
,
The main use of the /dev/fd files is from the shell. It allows programs that use pathname arguments to handle standard input and standard output in the same manner as other pathnames.
In my OS (Ubuntu), the Bash uses /dev/fd
for process substitution.
$ ls -l <(echo .)
lr-x------ 1 user user 64 12月 19 11:19 /dev/fd/63 -> pipe:[4926830]
Bash replaces <(echo .)
with /dev/fd/63
.
The above command is like ls -l /dev/fd/63
.
Or find the backing file via,
$ echo <(true)
/dev/fd/63
(After my Bash does the substitution, the command becomes echo /dev/fd/63
,
which outputs /dev/fd/63
.)