When a guide says “works on macOS 15.2 and later”, I want my version without opening a menu. One command prints it.
sw_vers
sw_vers -productVersion
sw_vers -buildVersion
Plain sw_vers prints the product name, the version and the build, three lines. -productVersion and -buildVersion print only that value, which is exactly what you want in a script. The man page also documents -productName and -productVersionExtra.
The build number is the one you need for beta releases, or when Apple ships two builds with the same version number. It looks like 22A100, and bug reports and release notes often name it.
A small script that checks a minimum version:
[ "$(printf '%s\n' 14.0 "$(sw_vers -productVersion)" | sort -V | head -n1)" = 14.0 ] && echo ok
The caveat is that the sort -V trick needs a sort that supports version sort, which recent macOS has. And do not read the kernel version from uname -r as the macOS version: they are different numbers. Apple’s own man page for sw_vers mentions an environment variable for old compatibility behaviour, and a developer forum thread says newer macOS versions no longer honour it, so the output reflects the real system version.
Further reading: sw_vers(1) manual