utility

diff — external utility

Compares files and can produce a human-readable patch with -u. Status 0 means no difference, 1 means different, and greater than 1 indicates trouble. In scripts explicitly distinguish differences from errors. This example prints status rather than variable timestamp headers. Run in a fresh private directory with HOME set to that directory and LC_ALL=C. Expected output (one item per line): status=1.

Formal Syntax & Structure

bash syntaxbash
diff [-u] [--] FILE1 FILE2

Parameters & Attributes

NameTypeDescription
FILE1pathFirst file.
FILE2pathSecond file.

Return Value

exit status

0 for equal inputs, 1 for differences, greater than 1 for errors.

Working Example

Example Snippetbash
printf 'a\n' > beforeprintf 'b\n' > afterif diff -- before after > report; then exit 1; else printf 'status=%s\n' "$?"; figrep -Fxq '< a' report && grep -Fxq '> b' report || exit 1
Bash Reference