Process substitution allows a program to read the output of another program as though the output were a file. In a
shell, the command for the program producing the output is be embedded in
<( … )
.
/dev/fd
The output of a program is made available through a special «file» under
/dev/fd/
as is demonstrated with the following shell script (
show-filenames-and-their-content.sh
):
for file in "$@"; do
if [ -e "$file" ]; then
echo Reading content of $file
while IFS= read -r line || [ -n "$line" ]; do
echo " $line"
done < "$file"
else
echo Not reading $file
fi
done
Show the filename of a real file:
$ ./show-filenames-and-their-content.sh /etc/passwd
Reading content of /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
…
Show the filename of a file read with process substitution:
$ ./show-filenames-and-their-content.sh <(/bin/sh -c 'echo a; echo b')
Reading content of /dev/fd/63
a
b