Process Substitution Diff Streams

Compare two command outputs without temp files using bash process substitution with the diff command.

BashCli Tooling

Code

bashsnippet
9 lines
#!/usr/bin/env bash
set -euo pipefail

diff <(sort file_a.txt) <(sort file_b.txt) \
  --side-by-side \
  --width=120 \
  --suppress-common-lines

echo "Exit code: $?"

Output

Captured from a sandboxed run of this snippet in the build pipeline.

Exit code: 0

How it works

Strict mode (set -euo pipefail) makes failures loud — an unset variable or a failing pipeline stage aborts the script instead of being swallowed.

The work is delegated to diff, chained so each stage’s output feeds the next.

Interactive Tools for This Topic

FAQ

Does the script stop on the first error?
Yes — `set -euo pipefail` aborts on the first failing command, treats unset variables as errors, and fails the whole pipeline if any stage fails. Nothing continues half-broken.
Which external commands does the script rely on?
`diff` must be available on the PATH environment variable; everything else is shell built-in syntax.