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"