In computing, process substitution is a form of inter-process communication that allows the input or output of a command to appear as a file. The command is substituted in-line, where a file name would normally occur, by the command shell. This allows programs that normally only accept files to directly read from or write to another program.
History
Process substitution was available as a compile-time option for ksh88, the 1988 version of the Korn shell from Bell Labs. The rc shell provides the feature as "pipeline branching" in Version 10 Unix, released in 1990. The Bash shell provided process substitution no later than version 1.14, released in 1994.
Example
The following examples use Korn shell syntax. The Unixdiff command normally accepts the names of two files to compare, or one file name and standard input. Process substitution allows one to compare the output of two programs directly: $ diff < <
The < expression tells the command interpreter to run command and make its output appear as a file. The command can be any arbitrarily complex shell command. Without process substitution, the alternatives are: Both alternatives are more cumbersome. Process substitution can also be used to capture output that would normally go to a file, and redirect it to the input of a process. The Bash syntax for writing to a process is >. Here is an example using the tee, wc and gzip commands that counts the lines in a file with wc -l and compresses it with gzip in one pass: $ tee > < bigfile | gzip > bigfile.gz
Advantages
The main advantages of process substitution over its alternatives are:
Performance: Reading directly from another process is often faster than having to write a temporary file to disk, then read it back in. This also saves disk space.
Parallelism: The substituted process can be running concurrently with the command reading its output or writing its input, taking advantage of multiprocessing to reduce the total time for the computation.
Mechanism
Under the hood, process substitution has two implementations. On systems which support /dev/fd it works by calling the pipesystem call, which returns a file descriptor $fd for a new anonymous pipe, then creating the string /dev/fd/$fd, and substitutes that on the command line. On systems without /dev/fd support, it calls mkfifo with a new temporary filename to create a named pipe, and substitutes this filename on the command line. To illustrate the steps involved, consider the following simple command substitution on a system with /dev/fd support: $ diff file1 <
The steps the shell performs are:
Create a new anonymous pipe. This pipe will be accessible with something like /dev/fd/63; you can see it with a command like echo <.
Execute the substituted command in the background, piping its output to the anonymous pipe.
Execute the primary command, replacing the substituted command with the path of the anonymous pipe. In this case, the full command might expand to something like diff file1 /dev/fd/63.
When execution is finished, close the anonymous pipe.
For named pipes, the execution differs solely in the creation and deletion of the pipe; they are created with mkfifo and removed with unlink. All other aspects remain the same.
Limitations
Process substitution has some limitations:
No file seeking: the "files" created are not seekable, which means the process reading or writing to the file cannot perform random access; it must read or write oncefrom start to finish. Programs that explicitly check the type of a file before opening it may refuse to work with process substitution, because the "file" resulting from process substitution is not a regular file.
No exit codes: "It is not possible to obtain the exit code of a process substitution command from the shell that created the process substitution."