Output of "${OSTYPE:6}" on old releases of Mac OS X

I'm writing a shell script that I want to be theoretically compatible with very old versions of Mac OS X (I say "theoretically", because I lack any super-old machines to actually test with). The script uses the output of ${OSTYPE:6} to determine which release of Mac OS X it's running on.

On a machine running 10.13 High Sierra, echo ${OSTYPE:6} returns 17. On a machine running 10.10 Yosemite, it returns 14. Logic thus suggests that the command would output "15" on 10.11 and "16" on 10.12.

(This is useful, because it allows me to include lines like if [[ ${OSTYPE:6} -ge 14 ]]; then DoThing; fi to run DoThing on any system running Yosemite and newer.)

Does this pattern hold for old releases as well? For example, would the command return "4" on the original 10.0?

I ask because, well, it would be odd to start at 4, so I'm worried that somewhere in history, the number was incremented by a point update.


That sounds very much like it's returning the Darwin version.

Your theory works well from 10.15 Mojave [18] back to 10.2 Jaguar [6] but fails before that, as Darwin was 1.x.x at that point.

There's a full list at Wikipedia - macOS


On a machine running 10.13 High Sierra, echo ${OSTYPE:6} returns 17...

Here's an old PowerMac G5 running OS X 10.5 circa 2009:

$ bash --version
GNU bash, version 3.2.17(1)-release (powerpc-apple-darwin9.0)

$ sh --version
GNU bash, version 3.2.17(1)-release (powerpc-apple-darwin9.0)

$ echo ${OSTYPE:6}
9.0

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.5.8
BuildVersion:   9L31a

$ system_profiler SPSoftwareDataType
Software:

    System Software Overview:

      System Version: Mac OS X 10.5.8 (9L31a)
      Kernel Version: Darwin 9.8.0
      Boot Volume: Macintosh HD
      Boot Mode: Normal
      Computer Name: PowerMac
      User Name: Jeffrey (jwalton)
      Time since boot: 66 days 3:46

I've never used if [[ ${OSTYPE:6} -ge 14 ]]; then DoThing; fi. However, I've used similar to this in shell scripts and Makefiles:

IS_OLD_DARWIN=$(system_profiler SPSoftwareDataType 2>/dev/null | grep -i -c -E "OS X 10\.[0-5]")
if [[ "$IS_OLD_DARWIN" -ne 0 ]]; then DoThing; fi

I can provide remote SSH access to the PowerMac. I use it for testing open source libraries, like Crypto++ and OpenSSL. Email me at noloader, gmail account if you want access.

As far as I know, the oldest version of Bash you can get your hands on for testing is Bash 2.x circa 2004. Download Fedora 1 from /pub/archive/fedora/linux/core.


I don't have a 10.0 machine to test on, but on 10.3 and 10.5 machines, echo ${OSTYPE:6} returns a blank line ($OSTYPE on both is darwin); on 10.6, it returns 10.0.

(Incidentally, your shell script would need to explicitly invoke bash. The default shell on very old versions of OSX is tcsh, which doesn't understand the ${OSTYPE:6} syntax.)