KKAIROS
Practical guideIllustrative3 min read

diff: see what changed between two files

diff reports line differences between two inputs and exits 0 when they match, 1 when they differ, 2 on trouble.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-42-A: diff exit status: 0 same, 1 different, 2 trouble

Setup for this run, inside a throwaway directory (mktemp -d):

printf 'a\n' > x; printf 'a\n' > y; printf 'b\n' > z

Command:

diff -q x y >/dev/null; echo same=$?; diff -q x z >/dev/null; echo diff=$?; diff -q x missing >/dev/null 2>&1; echo trouble=$?

Recorded output:

same=0
diff=1
trouble=2

TE-42-B: diff -u shows a changed line as a minus line and a plus line under a hunk header

Setup for this run, inside a throwaway directory (mktemp -d):

printf 'a\n' > x; printf 'b\n' > z

Command:

diff -u x z | tail -n +3

Recorded output:

@@ -1 +1 @@
-a
+b

Exit status: 0. This status and the output match what was written before the run. The status is that of tail, the last command in the pipe. diff -u itself exits 1.

TE-42-C: expected non-zero exit: diff -q reports that two files differ and exits with status 1

Setup for this run, inside a throwaway directory (mktemp -d):

printf 'a\n' > x; printf 'b\n' > z

Command:

diff -q x z

Recorded output:

Files x and z differ

Exit status: 1. This status and the output match what was written before the run.

TE-42-B: diff -u prints two header lines first, and tail -n +3 removed them. What is left is the hunk header @@ -1 +1 @@, the removed line -a and the added line +b. TE-42-C: by the setup, x holds a and z holds b. diff -q x z printed Files x and z differ and exited with status 1, which means the files differ.

Limits

A difference is a normal result, not a failure.

Ad

Source

Evidence

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-42-A | diff exit status: 0 same, 1 different, 2 trouble | 0 | PASS | ff8cf62d1b76 | | TE-42-B | diff -u shows a changed line as a minus line and a plus line under a hunk header | 0 | PASS | bb380314e71c | | TE-42-C | expected non-zero exit: diff -q reports that two files differ and exits with status 1 | 1 | PASS | 7c9707b05766 |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides