Check OS is Mac in Bash

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

$ echo $OSTYPE

As you can see, it starts with darwin.


To check using an if statement:

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

Logical Operator

To check using a one-line logical operator:

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

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