Search notes:

Process substitution

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 <( … ).

Simple example

The following example compares the file and directory names in two directories:
$ diff  <( ls -1 ~/dir-one )  <( ls -1 ~/dir-two )

/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

See also

Using process substition with pr to print the output of two programs side by side.

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Linux/sh...', 1745939740, '18.220.96.228', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Linux/shell/Process-substitution(80): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78