I'm trying to create a bash script that runs a command based on OS version that way I can deploy it to machines running 10.12 and 10.13 the only problem is depending on the version the command needs to be slightly different. How can I go about this?


1 Answer 1


You can get the OS version using sw_vers -productVersion.

$ sw_vers -productVersion

You can test on this in a shell script using

if [[ $(sw_vers -productVersion) == 10.13.2 ]]; then …

To test any 10.12 or 10.13 version, you can use parse the output such as with this

if [[ $(sw_vers -productVersion | sed "s/10\.\([0-9][0-9]\).*/\1/") == 13 ]]; then …

You must log in to answer this question.

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