brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: diff <(cmd1) <(cmd2) compares output without temp files

Diff
~/tips/tip-shell-process-substitution-diff

diff

Compares the output of two commands directly, without saving them to temporary files.

Comparing the output of two commands usually means two temp files and a cleanup. Process substitution skips all of it.

<(command) runs the command and gives you a filename that you can read its output from. Anything that expects a file can take it. The manual describes it as the process’s output appearing as a filename.

diff <(sort a.txt) <(sort b.txt)
diff <(ssh web1 cat /etc/hosts) <(ssh web2 cat /etc/hosts)
comm -12 <(sort one.txt) <(sort two.txt)

The first line compares two files after sorting them, without touching the originals. comm in particular wants sorted input, and this is how you give it that.

Caveats. It is a Bash, Zsh and Ksh feature, not POSIX, and it needs a system with /dev/fd or named pipes. You cannot use it in sh scripts. The name you get is a pipe, so you can only read it once and cannot seek, which breaks programs that open a file twice. No space may appear between < and the parenthesis. With a space it is a plain redirect plus a syntax error.

It also works the other way around. >(command) gives you a filename to write to, for example tee >(gzip > out.gz) to send one stream to two places.

Further reading: Bash manual: Process Substitution, On this site: trap EXIT cleans up your temp files, even when the script dies, On this site: use |& to pipe stderr along with stdout in Bash

More Diff tips diff -r -x .git -x '*.log' one two diff -y -W 100 old.conf new.conf diff -Naur original/ changed/ > fix.patch patch -p1 --dry-run < ../fix.patch diff --strip-trailing-cr windows.txt unix.txt All Diff tips (12) All topics

Link