$_ CLI In Depth
Learning progress 0%
lesson streams redirection pipes

Process Substitution

4 min read Module 7: Streams, Redirection & Pipes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:19

Two forms, one idea

Bash has two forms of process substitution. The form <(command) runs the command in the background, with its standard output connected to a pipe, and substitutes a path that names the read end of that pipe. The form >(command) does the reverse: it substitutes a path that names the write end, so anything written to that path flows into the command. The inner command runs in its own process, and the outer command simply sees a file.

The substituted path is typically under /dev/fd, which is a directory of links to the open descriptors of the current process. The exact number is not important, and you should never rely on it.

BASH1 lines
echo <(true)

The output is a path such as /dev/fd/63. The number changes from one run to the next, because it is simply the descriptor bash opened for the pipe. The true command has already exited, but the path stays valid for the reader.

Comparing two outputs

A classic use is to compare the output of two commands with diff. The diff utility wants file names, and process substitution provides them without any files on disk. Each side is produced by its own command, and diff reads both as ordinary files.

BASH1 lines
diff <(printf 'a\nb\n') <(printf 'a\nc\n')

The output shows the change that turns the first stream into the second: line b is replaced by c. Without process substitution you would need two temporary files, plus cleanup code to remove them afterward.

Reading into a loop without a subshell

Earlier you saw that a loop at the end of a pipeline runs in a subshell, so variables set inside it are lost. Process substitution fixes this, because the loop reads from a file path and runs in the current shell.

BASH3 lines
count=0
while read -r x; do count=$((count+1)); done < <(printf 'a\nb\n')
echo "count=$count"

The output is count=2. The loop was not in a pipeline, so its changes to count stayed in the shell that printed the result. The redirection from the process substitution provided the input.

Tip

When a loop must update variables, prefer done < <(command) over command | while ...; done. The first keeps the loop in the current shell.

Writing into a command

The form >(command) is less common but just as useful. It lets one command write into another that is expecting a file name. The example below sends a stream to a counter while also printing it, using tee to copy the data.

BASH2 lines
printf 'x\ny\n' | tee >(wc -l > count.txt) > /dev/null
cat count.txt

The file count.txt contains 2. The tee copy went into the process substitution, and wc wrote its result to a file. The shell waits for the inner command, but it is not guaranteed to wait before the next command starts, so scripts that depend on the file should wait for it explicitly.

Combining several sources into one stream

A process substitution can stand wherever a file name is expected, so a command can read several generated streams at once. The example concatenates two small generated lists and counts the lines, without writing either list to disk.

cat <(printf 'a\nb\n') <(printf 'c\n') | wc -l

The count is 3. The two producers ran concurrently, and cat read them in the order they appeared on its command line, so the output keeps the argument order even though the processes run at the same time.

Limits and portability

Process substitution is a bash feature, and it is not part of the POSIX shell language. Scripts that must run under sh on minimal systems should use named pipes or temporary files instead. Inside bash it is safe, and it does not require any extra permissions.

Another limit is that the substituted path is a pipe, not a regular file. Programs that seek, check the file size, or reopen it by name may misbehave. Most tools that read a file sequentially work well with it, but a tool that needs random access does not.

Hands-on: sort two lists and compare

Use process substitution to sort two lists on the fly and find the lines that appear only in the first. The comm utility compares two sorted streams, so the inputs are sorted inside the substitutions.

BASH1 lines
comm -23 <(printf 'b\na\nc\n' | sort) <(printf 'c\n' | sort)

The output is a and b, the lines that appear in the first list but not in the second. Each list was sorted in its own process, and comm read both as files. This is a compact replacement for a multi-step script with temporary files.

Terminal lab · 3 steps · real Linux in your browser

Feed commands as files

Compare two streams, count inside a loop, and join generated lists.

01
Diff two generated streams
Compare two small lists without creating any temporary files. Save the diff output: ``bash diff <(printf 'a\nb\n') <(printf 'a\nc\n') > diff.txt ``
Show hint for step 1
Each <( ) becomes a path that diff can open like a file.
02
Count in a loop that keeps its variable
Read three lines with a loop that takes its input from a process substitution, so the count survives the loop: ``bash count=0 while read -r x; do count=$((count+1)); done < <(printf 'a\nb\nc\n') echo "$count" > count.txt ``
Show hint for step 2
Redirect the loop from <( ) rather than piping into it.
03
Join two generated streams
Concatenate two generated lists into joined.txt: ``bash cat <(printf 'a\nb\n') <(printf 'c\n') > joined.txt ``
Show hint for step 3
cat accepts several file names, and each process substitution is one of them.
Check yourself Question 1 of 4
Concept Check What <(command) produces recall

What does the expression <(command) produce in bash?

Recap & Key Takeaways

The form <(command) substitutes a path that reads the command's output, and >(command) substitutes a path whose input goes to the command.
Use process substitution with diff, comm, and other tools that take file names, so no temporary files are needed.
Feeding a loop with done < <(command) keeps the loop in the current shell, so its variables survive.
Process substitution is a bash feature, and the substituted path is a pipe rather than a regular file.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue