document updated 12 years ago, on Oct 4, 2012
Often I want to diff the output of two COMMANDS. That is, I want to run command A, then run command B, then diff the output of both of those.
For example:
sort file1 > file1.sorted
sort file2 > file2.sorted
diff file1.sorted file2.sorted
Can this be done without writing the output to a temporary file?
Answer: YES!
The trick is to use Bash's process substitution feature:
diff <(sort file1) <(sort file2)
Bash changes that into:
diff /dev/fd/63 /dev/fd/62
(you can tell by running echo diff <(sort file1) <(sort file2))
It does that ^^^ after first running the two sort commands, and sending their output to filehandles 62 and 63.
show me more!
Want to do more magic with process substitution? See the following examples:
multitee is something else to look into
show me more!
There's lots of other I/O redirection tricks.
[1]
[2]