snippetbashTippending
Shell: Process substitution and named pipes
Viewed 0 times
process substitutionnamed pipediff commandsfile descriptor
Problem
Need to compare outputs of two commands, or use command output where a file is expected.
Solution
Process substitution creates temporary file descriptors from command output:
Note:
# Compare output of two commands
diff <(sort file1.txt) <(sort file2.txt)
# Compare two API responses
diff <(curl -s api.example.com/v1/users | jq .) \
<(curl -s api.example.com/v2/users | jq .)
# Feed multiple inputs to a command
paste <(cut -f1 file1.tsv) <(cut -f3 file2.tsv)
# Use with while read (avoids subshell issue with pipes)
while IFS= read -r line; do
count=$((count + 1))
done < <(grep -r 'TODO' src/)
echo "Found $count TODOs" # count is correct!
# Write to multiple destinations
tee >(gzip > backup.gz) >(wc -l > count.txt) < input.txtNote:
<() creates a readable fd, >() creates a writable fd.Why
Process substitution avoids temporary files and subshell variable scoping issues that occur with pipes.
Gotchas
- Not available in sh/dash - bash/zsh only
- Cannot seek in process substitution file descriptors
Context
Shell scripting when you need command output as file input
Revisions (0)
No revisions yet.