How can I identify the macOS version from the command line? [duplicate]

After detecting a GNU/Linux system through running uname, in a GNU/Linux distribution lsb_release -a is a good entry level utility you could use to [try to] find out the GNU/Linux distribution name and particular version1.

After detecting a Darwin (possibly macOS) system through running uname, there must be a Different (maybe not that much...) way to Ask the OS version number1. Even if it only works on macOS, it'd be useful.

This is similar to the /etc/release for other Unix / Linux, but I’d like to specifically target macOS to script a check of the version. In what way(s) is this possible?


Here are some other options that you could use.

sysctl kern.version
kern.version: Darwin Kernel Version 16.7.0: Sun Jun  2 20:26:31 PDT 2019; root:xnu-3789.73.50~1/RELEASE_X86_64

sysctl kern.ostype
kern.ostype: Darwin

sysctl kern.osrelease
kern.osrelease: 16.7.0

sysctl kern.osrevision
kern.osrevision: 199506

And as an alternative to sw_vers

/usr/libexec/PlistBuddy -c "Print:ProductName" \
                        -c "Print:ProductVersion" \
                        -c "Print:ProductBuildVersion" /System/Library/CoreServices/SystemVersion.plist
Mac OS X
10.12.6
16G2128

I don't know if all of this is imposed by Darwin, the manual page seems like it maybe is a Mac OS X (sic in my manual page as of Sep 2019) thing, but there is the awesome sw_vers:

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14.6
BuildVersion:   yadda yadda yadda
$ _

LSB stands for "Linux Standard Base".

macOS isn't Linux, so it isn't part of the LSB project, and doesn't support LSB tools.


Use the standard way - uname - defined in POSIX so works in all Unix systems and sopme others that provide compatibility.

Examples from my Mac mini running macOS 10.14.6

uname -a gives all it can

Darwin mini12 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64

Given the question you link to the full answer is possibly alter the script in https://stackoverflow.com/a/56968182/151019 The code for macOS is the same as for Solaris except change the DIST and OS values

uname -v prints the operating system version. (actual POSIX definition is Write the current version level of this release of the operating system implementation.) For macOS it prints the Darwin version. The Linux man page says it prints the kernal version. (Solaris has the same definition as the macOS man page so I assume it is the version of the Solaris kernal.)

Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64

and uname -r just gives the number (POSIX definition - Write the current release level of the operating system implementation.)

18.7.0