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.

Conditional

To check macOS using an if statement:

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

Logical Operator

To check macOS using a one-line logical operator:

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

uname

Alternatively, you can check OS type using uname:

uname # Darwin


Please support this site and join our Discord!