Process Substitution for Pipelines

Uses process substitution to feed command output as files to commands expecting file arguments, enabling complex data comparison and merging operations.

BashCli Tooling

Code

snippet
#!/usr/bin/env bash
set -euo pipefail

# Compare command outputs without temp files
diff <(ls /etc/*.conf 2>/dev/null | sort) <(ls /etc/*.cfg 2>/dev/null | sort) || true

# Feed generated data to commands expecting files
sort <(echo -e "cherry\napple\nbanana")

# Merge sorted streams
sort -m <(echo -e "1\n3\n5") <(echo -e "2\n4\n6")

# Process substitution with while loop
while read -r line; do
    echo "Processing: $line"
done < <(seq 1 5)

# Use with paste to combine columns
paste <(seq 1 5) <(seq 6 10) | while read -r a b; do
    echo "$a + $b = $((a + b))"
done

# Tee with process substitution for logging
echo "Important data" | tee >(gzip > /tmp/output.gz) > /tmp/output.txt

echo "Process substitution complete"

Feeding process output into a pipeline

set -euo pipefail removes the shell’s usual optimism — errors abort, empty variables abort, hidden pipeline failures abort.

The work is delegated to diff, sort and paste, chained through 6 pipes so each stage’s output feeds the next.

Caveats

  • The pipeline is 6 stages deep — when it misbehaves, split the pipe and test the stages separately.

Statements that matter

diff <(ls /etc/*.conf 2>/dev/null | sort) <(ls /etc/*.cfg 2>/dev/null | sort) || true
while read -r line; do
echo "Processing: $line"
paste <(seq 1 5) <(seq 6 10) | while read -r a b; do
echo "$a + $b = $((a + b))"

Interactive Tools for This Topic

FAQ

Does the script stop on the first error?
Yes — `set -euo pipefail` aborts on the first failing command, treats unset variables as errors, and fails the whole pipeline if any stage fails. Nothing continues half-broken.
Which external commands does the script rely on?
`diff`, `sort`, `paste` must be available on the PATH environment variable; everything else is shell built-in syntax.