In computing, process substitution is a form of inter-process communication that allows the input or output of a command to be made available as a file path. The command is substituted in-line, where a file name would normally occur, by the command shell. This allows programs that normally only accept file paths to directly read from or write to another program.
Process substitution was first introduced by the KornShell from Bell Labs and first documented in <code>ksh86</code> from 1986 initially only available on systems with support for the <code>/dev/fd/<i>n</i></code> character device files. The rc shell provides the feature as "pipeline branching" in Version 10 Unix, released in 1990 with a different syntax. The Z Shell has had the feature since its initial 1.0 version released in 1990 and the Bash shell since version 1.13.4, released in 1993 both using the Korn Shell syntax.
Zsh (since its first version in 1990), added a third form of process substitution (<code>=(cmd)</code>) that uses a temporary file instead of a pipe. The fish shell added its own process substitution with yet another syntax in 2005, using pipes, then a variant using temporary files in 2007 which then became the default (initially inadvertently) as the pipe variant suffered from deadlocks.
The following examples use KornShell syntax.
The Unix diff 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:
The <code><(command)</code> expression tells the command interpreter to run command and make its output appear as a file. The command can be any arbitrarily complex shell code.
Without process substitution, the alternatives are:
All of which 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 KornShell syntax for writing to a process is <code>>(command)</code>. Here is an example using the <code>tee</code>, <code>wc</code> and <code>gzip</code> commands that counts the lines in a file with <code>wc -l</code> and compresses it with <code>gzip</code> in one pass:
The main advantages of process substitution over its alternatives are:
Under the hood, process substitution variants where commands run concurrently have two possible implementations. On systems which support <code>/dev/fd</code> (most Unix-like systems) most implementations work by calling the <code>pipe()</code> system call, which returns a file descriptor <code>$fd</code> for a new anonymous pipe, then creating the string <code>/dev/fd/$fd</code>, and substitutes that on the command line. On systems without <code>/dev/fd</code> support, they call <code>mkfifo()</code> with a new temporary filename to create a named pipe, and substitute this filename on the command line. To illustrate the steps involved, consider the following simple command substitution on a system with <code>/dev/fd</code> support:
The steps the shell performs are:
For named pipes, the execution differs solely in the creation and deletion of the pipe; they are created with <code>mkfifo()</code> (which is given a new temporary file name) and removed with <code>unlink()</code>. All other aspects remain the same.
The variants using temporary files such as Zsh's <code>cmd1 =(cmd2)</code> or Fish's <code>cmd1 (cmd2|psub -f)</code> first run with its output redirected to a temporary file, then run with substituted with the path of that temporary file and automatically delete that file after terminates.
Except for the variants using temporary files, the "files" created are not seekable nor mmap'apble, which means the process reading or writing to the file cannot perform random access; it must read or write once from 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. Additionally, in the KornShell, Bash versions prior to 4.4 (2016) and Zsh versions prior to 5.6 (2018), it is not possible to obtain the exit status of a process substitution command from the shell that created the process substitution.