Overview
sum prints a 16-bit checksum of a file and the number of 1024-byte blocks it takes. It is an old command, kept for compatibility. For new uses, cksum or sha256sum are better, because a 16-bit sum misses many changes.
Background
Today the name covers GNU sum, the sum that macOS ships, and the sum applet in BusyBox. The macOS manual has no dated history for it. All three use the BSD checksum algorithm by default, so they agree on the number.
Examples
Print the checksum and block count for some text:
printf abc | sum
Print them for a file; awk keeps just the two numbers, because the file name is not printed on every system:
printf abc > f.txt && sum f.txt | awk '{print $1, $2}'
Compare two files by checksum:
printf abc > a.txt && printf abc > b.txt && test "$(sum a.txt | cut -d' ' -f1)" = "$(sum b.txt | cut -d' ' -f1)" && echo same
Common mistakes
A 16-bit checksum has only 65,536 values, so two different files will often match by chance; do not use sum to prove files are equal. The spacing of the output is not the same on all systems, so split on whitespace. The block count is rounded up, so a 3-byte file counts as 1.
Portability
Only the default BSD algorithm is in the Common examples. GNU also has -s for the System V algorithm, which gives different numbers. The Common examples were executed on GNU coreutils 9.11, macOS system tools, and BusyBox 1.37.0 (see the development guide for the exact check).
About macOS sum
This is the sum in the system folders of macOS. It follows the BSD implementation.
Apple notes
macOS sum prints the BSD checksum and block count, and does not have --sysv.
References
Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.