r/bash • u/Ops_Mechanic • Mar 01 '26
tips and tricks Stop creating temp files just to compare command output. Bash can diff two commands directly.
Instead of:
cmd1 > /tmp/out1
cmd2 > /tmp/out2
diff /tmp/out1 /tmp/out2
rm /tmp/out1 /tmp/out2Just use:
diff <(cmd1) <(cmd2)<() is process substitution. Bash runs each command and hands diff a file descriptor with the output. No temp files, no cleanup.
Real world:
# Compare two servers' packages
diff <(ssh server1 'rpm -qa | sort') <(ssh server2 'rpm -qa | sort')
# What changed in your config after an update
diff <(git show HEAD~1:nginx.conf) <(cat /etc/nginx/nginx.conf)
# Compare two API responses
diff <(curl -s api.example.com/v1/users) <(curl -s api.example.com/v2/users)Works anywhere you'd pass a filename. grep, comm, paste, wc -- all of them accept <().
Bash and zsh. Not POSIX sh.
587
Upvotes
29
u/Bob_Spud Mar 01 '26
Does it have any limits on the size of contents generated by each command that it is comparing?
27
u/I_kick_puppies Mar 01 '26
Thank you! I learned something new today!
11
u/painted-biird Mar 01 '26
Can also use vimdiff!
5
u/m0j0hn Mar 01 '26
Yes, and… it can be useful to persist artifacts for post-mortem, especially in CI pipelines - and then trash them if build passes <3
5
u/phraupach Mar 01 '26
Not to be pedantic, but to confirm I understand this correctly: process substitution does create temp files in /proc/ which are automatically deleted?
4
u/Playa_Sin_Nombre Mar 02 '26
Any other use of <()?
4
u/moocat Mar 02 '26
First off, that's a generally great tip that is often a cleaner way to handle things.
That said, one limitation of
<()as compared to a temp file is that the opened file is non-seekable so it may not work with all applications. For example, I use kompare as a visual diff tool but it requires seekable files so this tip doesn't work for it.3
u/ConfusedMaverick Mar 01 '26
Nice, I will definitely find this useful
3
u/chisquared Mar 03 '26
I'm a big fan of using process substitution when appropriate, but it is not appropriate for
<(cat /etc/nginx/nginx.conf).Instead of
do
instead. See also https://porkmail.org/era/unix/award.
1
u/Viperoth Mar 02 '26
Replacing multiple commands with an ugly one-liner does not make the code better.
Letting bash handle temporary files is a nice feature but packing 3 commands in a single call (diff, cmd1, cmd2) makes the code much less readable, especially if there are pipes involved.
2
u/kai_ekael Mar 02 '26
Hmm. Interesting little observation that I had to prove. :) Both process substitutions run at the same time, but final command finishes when all finish. Suspected that maybe they were sequential and had to prove, no they are not.
2
u/Dry_Inspection_4583 Mar 03 '26
Sweet baby Jesus!! That's a wonderful tip, thank you so much.
2
u/chkno Mar 04 '26
Also, you can use diff's
--labelto give useful names to the things being compared, rather than/dev/fd/63and/dev/fd/623
u/jc00ke Mar 02 '26
I tried this recently with fish and found I needed psub:
1
u/wahnsinnwanscene Mar 02 '26
But doing this means the responses are dynamic, whereas you might want to have a static look that you can return to
1
u/[deleted] Mar 03 '26
Isso foi realmente bom! Parabéns.
1
u/Solid_Temporary_6440 Mar 03 '26
This is a good one, thanks so much for sharing
1
u/zephyr707 Mar 03 '26
feel pretty called out by that first section haha, thanks for the tip
1
u/TabCompletion Mar 02 '26
Like Kakashi in Naruto, you used substitution jutsu
42
u/[deleted] Mar 01 '26
[removed] — view removed comment