Process substitution
≈ 3 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
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.
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. Zsh (since its first version in 1990), added a third form of process substitution ( =(cmd) ) 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. $ 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 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 >(command) . 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 >(wc -l >&2) bigfile.gz $ 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 ( sort file2 in this case), piping its output to the anonymous pipe. Simultaneously, execute the primary command, replacing the substituted command with a path to 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() (which is given a new temporary file name) and removed with unlink() . All other aspects remain the same. The variants using temporary files such as Zsh's cmd1 =(cmd2) or Fish's cmd1 (cmd2|psub -f) 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.
Text derived from https://en.wikipedia.org/w/index.php?oldid=1375789811 under CC BY-SA 4.0; modifications have been made.