How to Use the macOS sw_vers Command to View Operating System Versions

The Version Confusion

If you are a normal user, finding out what version of macOS you are running is incredibly simple: you click the Apple logo in the top left corner of the screen and select “About This Mac.” A beautiful graphical window pops up and tells you if you are running macOS Sonoma, Ventura, or Monterey.

However, if you are a systems administrator writing an automated Bash script that needs to deploy a specific security patch, a graphical window is completely useless. Your script cannot click menus or read marketing names like “Sonoma.” Your script requires hard, mathematical data, formatted perfectly as plain text, to execute its logic.

If you need to programmatically extract the exact version number, build number, and product name of the operating system directly through the command line, you must use the sw_vers (Software Version) command.

Step 1: Open the Terminal

Because you are merely reading non-sensitive system data, you do not need root administrator privileges to run this command.

  1. Press Command + Space to open Spotlight Search.
  2. Type Terminal and press Enter.

Step 2: The Basic Data Dump

To pull the complete version matrix from the operating system’s core, type the command and press Enter:

sw_vers

The terminal will instantly output a clean, three-line block of text:

ProductName: macOS
ProductVersion: 14.3.1
BuildVersion: 23D60

This provides exactly what a script needs. It ignores the marketing name entirely and provides the raw mathematical version (14.3.1) and the highly specific compiled Build Version (which Apple engineers use internally to track micro-updates).

Step 3: Surgical Data Extraction

If you are writing a script that says, “If the Mac is running version 14.3 or higher, install the software,” the three-line output is actually too noisy. If you pipe that three-line output into your script, the script will get confused by the words “ProductName” and “BuildVersion.”

You need to surgically extract only the exact number you care about. The sw_vers command provides specific flags to isolate each line of data.

If you only want the exact mathematical version number (e.g., 14.3.1), use the -productVersion flag.

sw_vers -productVersion

The terminal will instantly output: 14.3.1

If you only care about the internal compilation code, use the -buildVersion flag.

sw_vers -buildVersion

The terminal will instantly output: 23D60

Step 4: Using sw_vers in Automation

The surgical extraction flags make sw_vers the ultimate tool for automated scripting.

Imagine you want to create a variable in your script that dynamically stores the version of whatever Mac the script is currently running on. You can use command substitution (the backticks or the $() syntax) to run sw_vers silently in the background and inject its output directly into the variable.

MAC_VERSION=$(sw_vers -productVersion)

Now, your script perfectly understands the environment it is operating in. It can mathematically compare the $MAC_VERSION variable against a known requirement, ensuring that critical software is never installed on an incompatible operating system. By mastering the sw_vers command, you give your automated scripts the ability to instantly read the DNA of the Mac they are running on.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.