Overview
basename removes the folders from a path and prints the last part. It can also remove a suffix, such as an extension. It works on the text of the path only, so the file does not have to exist.
Background
The basename and dirname utilities first appeared in 4.4BSD, according to the macOS manual. Today the name covers GNU basename, the basename that macOS ships, and the basename applet in BusyBox. They agree on a path, and on a suffix given as a second argument; they differ in the options for several names at once.
Examples
Print the file name of a path:
basename logs/server.log
Remove a suffix as well:
basename logs/server.log .log
A trailing slash is ignored:
basename /usr/local/bin/
Use it in a loop to build new names from old ones:
for f in logs/*.log; do echo "$(basename "$f" .log).txt"; done
Common mistakes
basename never looks at the file system: basename logs/missing.log prints missing.log. Quote a path with spaces, or it is split into several arguments. A suffix only matches at the very end, and a name that equals the suffix is left whole, so basename .log .log prints .log. In a shell script, ${path##*/} does the same without starting a process, but it is not what a trailing slash gives you.
Portability
Only a path with an optional suffix is in the Common examples. -a and -s for several names differ between implementations and are not in POSIX. 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 basename
This is the basename in /usr/bin on macOS. It follows the BSD implementation and also accepts -a and -s.
Apple notes
macOS basename supports -a and -s SUFFIX with the same meaning as GNU's.
References
Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.