Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Process substitution

From Wikipedia, the free encyclopedia
Feature of some Unix shells

In computing,process substitution is a form ofinter-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 thecommand shell. This allows programs that normally only accept files to directly read from or write to another program.

History

[edit]

Process substitution was available as a compile-time option forksh88, the 1988 version of theKornShell fromBell Labs.[1] Therc shell provides the feature as "pipeline branching" inVersion 10 Unix, released in 1990.[2] TheBash shell provided process substitution no later than version 1.14, released in 1994.[3]

Example

[edit]

The following examples use KornShell syntax.

TheUnixdiff 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<(sortfile1)<(sortfile2)

The<(command) expression tells the command interpreter to runcommand and make its output appear as a file. Thecommand can be any arbitrarily complex shell command.

Without process substitution, the alternatives are:

  1. Save the output of the command(s) to a temporary file, then read the temporary file(s).
    $sortfile2>/tmp/file2.sorted$sortfile1|diff-/tmp/file2.sorted$rm/tmp/file2.sorted
  2. Create anamed pipe (also known as aFIFO), start one command writing to the named pipe in the background, then run the other command with the named pipe as input.
    $mkfifo/tmp/sort2.fifo$sortfile2>/tmp/sort2.fifo&$sortfile1|diff-/tmp/sort2.fifo$rm/tmp/sort2.fifo

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>(command). Here is an example using thetee,wc andgzip commands that counts the lines in a file withwc -l and compresses it withgzip in one pass:

$tee>(wc-l>&2)<bigfile|gzip>bigfile.gz

Advantages

[edit]

The main advantages of process substitution over its alternatives are:

  • Simplicity: The commands can be given in-line; there is no need to save temporary files or create named pipes first.
  • 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 ofmultiprocessing to reduce the total time for the computation.

Mechanism

[edit]

Under the hood, process substitution has two implementations. On systems which support/dev/fd (most Unix-like systems) it works by calling thepipe() system 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 callsmkfifo 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:

$difffile1<(sortfile2)

The steps the shell performs are:

  1. Create a new anonymous pipe. This pipe will be accessible with something like/dev/fd/63; you can see it with a command likeecho <(true).
  2. Execute the substituted command in the background (sort file2 in this case), piping its output to the anonymous pipe.
  3. 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 likediff file1 /dev/fd/63.
  4. 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 withmkfifo (which is given a new temporary file name) and removed withunlink. All other aspects remain the same.

Limitations

[edit]

The "files" created are notseekable, which means the process reading or writing to the file cannot performrandom 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 aregular file.Additionally, up to Bash 4.4 (released September 2016), it was not possible to obtain the exit code of a process substitution command from the shell that created the process substitution.[4]

See also

[edit]

References

[edit]
  1. ^Rosenblatt, Bill; Robbins, Arnold (April 2002). "Appendix A.2".Learning the Korn Shell (2nd ed.). O'Reilly & Associates.ISBN 0-596-00195-9.
  2. ^Duff, Tom (1990).Rc — A Shell for Plan 9 and UNIX Systems.CiteSeerX 10.1.1.41.3287.
  3. ^Ramey, Chet (August 18, 1994).Bash 1.14 release notes. Free Software Foundation. Available in the  Gnu source archive of version 1.14.7 as of 12 February 2016.
  4. ^"ProcessSubstitution".Greg's Wiki. 22 Sep 2016. Retrieved2021-02-06.

Further reading

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Process_substitution&oldid=1224844992"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp