Bash: Check Operating System is Mac

To verify the operating system is macOS in Bash, check the environment variable $OSTYPE:

echo $OSTYPE # darwin18.7.0

Notice that the string contains darwin.


To check macOS using an if statement:

if [[ $OSTYPE == 'darwin'* ]]; then
  echo 'macOS'

Logical Operator

To check macOS using a one-line logical operator:

[[ $OSTYPE == 'darwin'* ]] && echo 'macOS'


Alternatively, you can check OS type using uname:

uname # Darwin

Please support this site and join our Discord!