Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash process substitution

Reference for Bash process substitution: use the output of a command as a file with <(cmd), feed a command with >(cmd), and avoid temporary files and pipe subshells.

Process substitution lets a command's output look like a file. <(command) runs the command and gives you a file name (usually /dev/fd/63) from which you can read its output. >(command) does the opposite: it gives a file name that, when written to, feeds the command's input.

It is useful when a program insists on file arguments, such as diff, comm or paste, and you have commands instead of files. It also keeps a loop in the current shell, so variables survive.

Terminal
diff <(sort a.txt) <(sort b.txt)
while read -r line; do ...; done < <(some_command)
tar cf >(gzip > out.tgz) dir

Try it

Bash Comparing command output

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Keeping variables and writing out

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
<(list)      # read the output of list through a file name
>(list)      # write to the input of list through a file name
Form Description
<(command) Replaced by the name of a file or named pipe that provides the command's standard output. Use it wherever a file to read is expected.
>(command) Replaced by a file name; whatever is written there becomes the command's standard input.
< <(command) Redirect standard input from the substituted file; note the space between the two < characters.

Returns: a path such as /dev/fd/63 (or a path in /tmp on systems without /dev/fd). The commands run asynchronously, in parallel with the main command.

Errors / edge cases: the feature exists in Bash, Zsh and Ksh but not in POSIX sh or dash. It requires /dev/fd or named pipes. The substituted path is a pipe, so it cannot be seeked or read twice.

Usage notes

  • Single use. The data is a stream: a program that reads the file twice or seeks backwards (some editors, unzip, file) will fail. Use a temporary file for those.
  • Space in < <(. <<(cmd) is a syntax error because << is the here-document operator. Write < <(cmd).
  • Better than a pipe into while. Because the loop is not part of a pipeline, assignments inside it stay visible afterwards. See Pipelines.
  • >(...) timing. The shell does not wait for >(command) processes in older Bash versions. Output files may be incomplete immediately afterwards; Bash 4.4 and later let you wait for the last one, and a short sleep is only a demo shortcut.
  • Exit status is lost. The status of the substituted command is not available in $?. If it matters, write its output to a file or use a coprocess.
  • Not in POSIX scripts. For #!/bin/sh scripts, use named pipes (mkfifo) or temporary files.
  • Several inputs: diff <(cmd1) <(cmd2) works because each substitution has its own file name; the same pattern fits comm, join and paste.
  • Quote the whole expression when you hand it to eval, ssh or another shell: the substitution is expanded by the shell that parses it.