Check OS is Mac in Bash


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

$ echo $OSTYPE
darwin18.7.0

As you can see, it starts with darwin.

Conditional

To check using an if statement:

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

Logical Operator

To check using a one-line logical operator:

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


If you enjoyed this post, please consider supporting this site!