Concept

Process Substitution

Definition

Process substitution is bash's syntactic feature that lets the output (or input) of a command be used in any position where a filename is expected. The syntax is <(command) for reading from a command's stdout and >(command) for writing to a command's stdin. The shell sets up a pipe, hands the command-end of the pipe to the inner process, and substitutes a filesystem path — typically something like /dev/fd/63 — into the surrounding command line. The outer tool opens that path believing it is a real file; under the hood it is reading from or writing to the pipe.

Process substitution generalizes the ordinary pipe. A pipeline A | B connects exactly one source to one sink; process substitution lets a tool like diff accept two pipelines as if they were two files: diff <(sort a.txt) <(sort b.txt).

Why it matters

How it works

When bash sees <(command) in a command line, it forks a child process to run command, opens a pipe to the child, and looks up the path that names that pipe in /dev/fd/ (on Linux) or via a named pipe in /tmp/ (where /dev/fd/ is not available). It then replaces the <(command) token with that path and runs the outer command. The outer tool sees what looks like a file path and calls open() on it; the kernel returns the read-end of the pipe; data flows from the inner command's stdout through the pipe into whatever the outer tool reads from that file descriptor.

The >(command) form is the mirror image: bash runs the inner command with its stdin connected to the pipe and substitutes a path that, when opened for writing, sends bytes to the inner command. A common pattern is tee >(gzip > out.gz) >(sha256sum > out.sha) > out.raw, which writes the same stream to three destinations — a gzipped file, a checksum file, and a raw file — in a single pass.

Where it goes next

Continue exploring

Tags