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.
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' > zCommand:
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=2TE-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' > zCommand:
diff -u x z | tail -n +3Recorded output:
@@ -1 +1 @@
-a
+bExit 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' > zCommand:
diff -q x zRecorded output:
Files x and z differExit 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.
Source
Evidence
TE-42-A: diff exit status: 0 same, 1 different, 2 trouble
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
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