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.
Code
#!/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
Date Difference CalculatorCalculate the number of days, weeks, whole months, and years between two ISO dates, computed in UTC.Diff Ignoring WhitespaceCompare two blocks of text line by line and separate true differences from lines that differ only in spacing, indentation, or line endings.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.