2022-06-03    Share on: Twitter | Facebook | HackerNews | Reddit

Bash - Determine if Script Runs on Linux, macOS or Other System

unameOut="$(uname -s)"
case "${unameOut}" in
    Linux*)     machine=Linux;;
    Darwin*)    machine=Mac;;
    CYGWIN*)    machine=Cygwin;;
    MINGW*)     machine=MinGw;;
    *)          machine="UNKNOWN:${unameOut}"
esac

echo ${machine}

if [ "$machine" == "Mac" ]; then
    # code for macOS platform        
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
    # code for GNU/Linux platform
fi

Credits: The solution from bash - How to check if running in Cygwin, Mac or Linux? - Stack Overflow