Issue:
I know that I can find my current OS version number by selecting “About This Mac” under the Apple menu, but is there a way to get that information from the command line (e.g., if I am logged in to a remote machine and don't have access to the Desktop menus)?
Solution:
Use the command sw_vers. For example,
$ sw_versProductName: Mac OS XProductVersion: 10.7.3BuildVersion: 11D50
The following command gives just the version number: sw_vers | grep 'ProductVersion:' | grep -o '[0-9]*\.[0-9]*\.[0-9]*'
B. Lake reminded me that the following command will produce the same result: sw_vers | grep 'ProductVersion:' | cut -f2
The latter command format offers slightly easier versatility than the former; for example, if you only need the second digit of the OS version number (which is a real example from a script I wrote), then you can use the command sw_vers | grep 'ProductVersion:' | cut -d. -f2
to extract the “7” from “10.7.3”.
OS Version Compatibility: Lion, Snow Leopard, Leopard, Tiger
Update Status: 05 February 2012
|