blog advertising is good for you


blog advertising is good for you
User login

How to Get the Version of Mac OS X From the CLI

Mac OS X stores the current marketing OS version (i.e. not the kernel revision number) in a property list in CoreServices. You don’t need to parse the plist to get it out.

Grep

You could to use grep creatively. It’s quick, it’s easy, and incredibly useful.

grep -A1 ProductVersion /System/Library/CoreServices/SystemVersion.plist

sw_vers

As noted in the comments, you can get an even cleaner version with the sw_vers command:

sw_vers -productVersion

system_profiler

Also as noted in the comments, you can get more detailed results with System Profiler. However they’re not as script-friendly as the sw_vers or grep methods:

system_profiler SPSoftwareDataType
Average rating
(1 vote)
About Adam Knight
Adam Knight's picture

Author Biography

Adam Knight is one of the founders of Mac Geekery and is a geek at heart. Programmer by day, hacker by night, his daily life revolves around the Macintosh platform, which he has been a user and programmer for since the early days of System 7 when his LCII replaced his Apple //c.

In-between tech jobs, he’s managed to learn the basics of any web hacker: PHP, MySQL, Perl, Apache, Linux, *BSD, and the intricacies of ./configure —prefix=~/bombshelter/. Today, codepoet is concentrating on blogging again, writing some software for the Mac by himself (including Notae) and for his company (such as Switchblade) and has a few other toys coming out soon.

Bug him over AIM or email [link fixed].

Why wouldn’t you just use ‘sw_vers’ ?

Why not use defaults, which will even work on binary .plists?

defaults read /System/Library/CoreServices/SystemVersion ProductVersion

It can also be achieved using the system_profiler command…

system_profiler -detailLevel mini | grep “System Version”

Your command brings back two lines of XML on my machine:

        <key>ProductVersion</key>
        <string>10.4.5</string>

An alternative approach might be the command:

system_profiler -detailLevel mini SPSoftwareDataType | grep "System Version"

which produces the slightly cleaner result:

      System Version: Mac OS X 10.4.5 (8H14)

Each approach would require a little bit more processing (perhaps awk) to strip out the surrounding text if you wanted to test against the result in a script. If you want an easier version of the command, it can be simplified to the following, although it will then run slightly slower (about one to two seconds on my machine, against almost instantaneous for the first version):

system_profiler | grep "System Version"

Or you could just use ‘sw_vers‘, which has been in OSX forever now, and does the work of parsing that file even more simply than grep does Smiling

system_profiler is a great way to get command-line information. Without parameters it prints out a wealth of information. But you can limit it as follows to see just the system software version.

Miss-Daisy:~ imacpr0n$ system_profiler SPSoftwareDataType
Software:

System Software Overview: System Version: Mac OS X 10.4.9 (8P2137) Kernel Version: Darwin 8.9.1 Boot Volume: Miss Daisy Computer Name: Miss Daisy User Name: iMac Pr0n

Better yet, use system_profiler.

$ system_profiler SPSoftwareDataType
Software:

    System Software Overview:

      System Version: Mac OS X 10.4.9 (8P135)
      Kernel Version: Darwin 8.9.0
      Boot Volume: Macintosh HD
      Computer Name: hostname
      User Name: Your Name (your_user)

It’s probably better to use ‘sw_vers’ for this (as it’s likely to exist across Mac OS X revisions whereas that particular plist may cease to exist in the future).

% sw_vers
ProductName: Mac OS X
ProductVersion: 10.4.9
BuildVersion: 8P135

Because sw_vers -productVersion is too easy?

Adam Knight's picture

Because I hadn’t come across that command and it was easier to parse out the plist data than the system_profiler data when I needed to use this. Smiling

That, however, works. Smiling

How does one find out if the computer is Intel or not? I need to find out if the Mac is running Rosetta.

Adam Knight's picture

Use the machine or uname -p commands to get the processor type.

Use the arch command to test for PPC/intel. It outputs either “ppc” or “i386”.

—DH

uname -r

Adam Knight's picture

Kernel version. This gives you the marketing version.

This works in Mac OS X, Red Hat Fedora Core and Ubuntu:

#!/bin/csh -f

if(-r /etc/fedora-release) then cat /etc/fedora-release
else if(-r /etc/lsb-release) then perl -ne ‘if(/DESCRIPTION/) { s/.“(.)”/$1/; print; }’ /etc/lsb-release
else if(-d /System/Library) then set version_plist=System/Library/CoreServices/SystemVersion.plist

foreach vol (/Volumes/*) if(-r “$vol/$version_plist”) then set name=`defaults read “$vol/${version_plist:r}” ProductName` set build=`defaults read “$vol/${version_plist:r}” ProductBuildVersion` set version=`defaults read “$vol/${version_plist:r}” ProductUserVisibleVersion` printf “%-16s %10s %-10s %10s\n” “${vol:t}” “$name” “$version” “(build $build)” endif end else echo “Unknown OS: `uname -s`” endif

sysctl kern.osrelease

Post new comment
The content of this field is kept private and will not be shown publicly.
9 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.