177

I know how to find Mac OS X version from GUI:

Apple Menu (top left) > About This Mac

Is there a Terminal command that will tell me Mac OS X version?

5 Answers 5

244

You have a few options:

sw_vers -productVersion 

system_profiler SPSoftwareDataType

Either will do what you need, and will have an output format that's parseable (if that's what you're after).

3
  • 4
    The first one only gives you the OS version (ie, "10.7.5"). The second one gives you a lot of additional information such as 32/64-bit.
    – Kent
    Commented May 27, 2013 at 22:52
  • 1
    Nice one! I was going made looking for lsb_release or something along those lines. Never would have spotted those scripts. :D
    – Alastair
    Commented Feb 8, 2014 at 0:43
  • The two commands do not output the version name like Monterey
    – Timo
    Commented Dec 14, 2022 at 14:12
10

The command sw_vers shows the version.

For older Mac OS's you can find useful information in Wikipedia.

5

If all you care about is the major version (10.10, 10.9), you can do

MAJOR_MAC_VERSION=$(sw_vers -productVersion | awk -F '.' '{print $1 "." $2}')

I use this in a couple of scripts that have to do different things if run on 10.8.x, 10.9.x and now 10.10.

1
  • 6
    Simpler: sw_vers -productVersion | cut -d '.' -f 1,2
    – waldyrious
    Commented Nov 18, 2016 at 12:56
3

If you're looking to split the macOS version number based on semantic versioning for script logic, here is a small snip of code I use

product_version=$(sw_vers -productVersion)
os_vers=( ${product_version//./ } )
os_vers_major="${os_vers[0]}"
os_vers_minor="${os_vers[1]}"
os_vers_patch="${os_vers[2]}"
os_vers_build=$(sw_vers -buildVersion)

# Sample semver output
echo "${os_vers_major}.${os_vers_minor}.${os_vers_patch}+${os_vers_build}"
# 10.12.6+16G29

You can use these variables in script logic to run different commands based on the version of macOS. This gives slightly more granular control down to the patch or build version.

# Sample bash code
if [[ ${os_vers_minor} -ge 11 ]]; then
    DMG_FORMAT=ULFO
elif [[ ${os_vers_minor} -ge 4 ]]; then
    DMG_FORMAT=UDBZ
else
    DMG_FORMAT=UDZO
fi
2

The information is stored in /System/Library/CoreServices/SystemVersion.plist, you don't need the sw_vers tool for that. Especially if you want to access the version of an external drive you attached and didn't boot from.

2
  • That's actually quite cool! When was this featurette added? It certainly works under Big Sur 11.7.9, but I wonder if it's the kind of thing that has always been there? Commented Jul 26, 2023 at 17:19
  • 1
    I think it's been there for ages, but i can't tell since when. There's a sample file at: opensource.apple.com/source/DarwinTools/DarwinTools-1/… and it states 10.4, which is pretty much from the earliest days of macOS
    – Karsten
    Commented Aug 2, 2023 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .