Overview
stat reports details about a file that ls hides or formats loosely: exact size, permission bits, type, owner and timestamps. Its output format is different on each implementation, and so is the option that chooses the fields, so the portable use is the exit status. Pick the format option in the GNU, Apple or BusyBox section for what you need.
Background
The stat utility appeared in NetBSD 1.6 and FreeBSD 4.10, according to the FreeBSD and macOS manuals. Today the name covers GNU stat, the stat that macOS ships, and the stat applet in BusyBox. They agree only on whether a file exists, and differ on the option that chooses the fields: GNU and BusyBox use -c, macOS uses -f, and the format letters do not overlap.
Examples
Test that a file exists. The output is discarded, and stat exits with a non-zero status for a missing file:
stat notes.txt > /dev/null && echo exists
Handle a missing file:
stat missing.txt 2> /dev/null || echo missing
Common mistakes
Parsing the default output of stat in a script breaks when you move between Linux and macOS. Choose the format option for the implementation in use, or use test -e, test -d and ls where they are enough. By default stat describes a symbolic link itself; -L describes its target.
Portability
GNU and BusyBox use -c FORMAT with letters such as %s for size and %a for the octal mode. macOS uses -f FORMAT with %z for size and %Lp for the octal mode. The two format languages do not overlap, so only the existence check is common. 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 BusyBox stat
BusyBox bundles stat as an applet, and anything beyond the common options depends on how it was built. This page's examples ran on BusyBox 1.37.0.
BusyBox examples
BusyBox stat takes the same -c format as GNU for the fields below:
stat -c '%s' notes.txt
stat -c '%a %n' notes.txt
Options
-c
Print using a format string instead of the default report. BusyBox supports the common GNU letters;
the set depends on the build.
References
Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.