Terminal Expert · Lesson 1 · On the house

Process substitution

Feed command output as a file with <( ) and >( ). Lesson 1 is free — on the house.

Lessons · Terminal Expert · Lesson 1 of 10 · On the house

Process substitution

Feed command output as a file with <( ) and >( ).

Illustration for Terminal
Lesson 1 is on the house.

Process substitution turns a command's output (or input) into a temporary file-like path. diff <(sort a.txt) <(sort b.txt) sorts each file in a subshell and gives diff two paths — no intermediate sorted files on disk.

<(cmd) is readable (cmd's stdout appears as a file). >(cmd) is writable (writes into the path are stdin to cmd). Under the hood you often see /dev/fd/N or a FIFO; treat the path as opaque and pass it to tools that need a filename.

Classic uses: compare two live pipelines (diff <(…) <(…)), pass a generated list where a tool insists on a file (wc -l <(find . -name '*.go')), or tee-style fan-in with >(…). Prefer this over temp files you must remember to delete when the data is short-lived.

Not every tool accepts /dev/fd paths (some reopen by name incorrectly). If a binary rejects the path, fall back to a real mktemp file. Process substitution needs bash/zsh — plain POSIX sh may not have it.

Misses: writing <(cmd) when you meant a pipe into stdin, nesting so deeply you cannot debug, or assuming the path survives after the command that created it finishes in every shell.

proc-subst

diff <(sort a.txt) <(sort b.txt)
wc -l <(find . -name '*.go')
cat <(echo one) <(echo two)
tee >(wc -l >/tmp/lines.txt) < input.txt >/tmp/copy.txt
Match this when you type.

Quiz

What does <(sort a.txt) provide to the outer command?

Quiz

Why use diff <(sort a) <(sort b) instead of sorting to disk first?

Quiz

What is >(cmd) for?

Quiz

When might process substitution fail you?

Check

Match the still for: Process substitution.

Open-book: the answers are on this page. Pass every quiz and check (5) to mark the lesson done. This visit: 0/5.