Code Reference

Process Substitution

Bash · Reference cheat sheet

Process Substitution

Bash · Reference cheat sheet


📋 Overview

Process substitution (<(…) and >(…)) exposes a command’s output or input as a temporary filename (usually /dev/fd/N). It lets commands that expect files work with streams—and avoids pipeline subshell issues when reading into the current shell.

🔧 Core concepts

FormMeaning
<(cmd)Read from cmd’s stdout as a file
>(cmd)Write to a file that pipes into cmd’s stdin
Diff two streamsdiff <(cmd1) <(cmd2)
vs pipePipe connects stdin; process sub gives a path

Requires bash/zsh/ksh support and /dev/fd (or named temps). Not POSIX sh.

💡 Examples

Compare outputs:

diff -u <(sort a.txt) <(sort b.txt)
comm <(ls dir1) <(ls dir2)

Read in current shell:

while IFS= read -r line; do
  echo "$line"
done < <(curl -fsS "$url")

Multiple inputs:

paste <(cut -f1 data.tsv) <(cut -f2 data.tsv)

Tee-like with >():

echo hello > >(tee saved.txt)
# or
cmd | tee >(sha256sum >&2) > out.bin

mapfile:

mapfile -t lines < <(grep -v '^#' config)

⚠️ Pitfalls

  • Not available in POSIX sh or some restricted environments.
  • The path is ephemeral; don’t store it for later use after the command ends.
  • &lt;(cmd) runs asynchronously; ensure consumers read fully.
  • Combining with sudo may break /dev/fd visibility—run the whole pipeline elevated carefully.
  • Prefer process substitution over cmd >tmp && use tmp for cleanliness, but temp files still win for huge reusable data.
  • Quoting: "&lt;(cmd)" becomes a literal—don’t quote the substitution form.

On this page