Back
I want to keep my script of .bashrc and .bash_login files in version control to run in different OS. Can anyone tell me how to detect the OS in Bash script?
Try the below code:
if [[ "$OSTYPE" == "linux-gnu"* ]]; then # ...elif [[ "$OSTYPE" == "darwin"* ]]; then # Mac OSXelif [[ "$OSTYPE" == "cygwin" ]]; then # POSIX compatibility layer and Linux environment emulation for Windowselif [[ "$OSTYPE" == "msys" ]]; then # Lightweight shell and GNU utilities compiled for Windows (part of MinGW)elif [[ "$OSTYPE" == "win32" ]]; then # I'm not sure this can happen.elif [[ "$OSTYPE" == "freebsd"* ]]; then # ...else # Unknown.fi
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# ...
elif [[ "$OSTYPE" == "darwin"* ]]; then
# Mac OSX
elif [[ "$OSTYPE" == "cygwin" ]]; then
# POSIX compatibility layer and Linux environment emulation for Windows
elif [[ "$OSTYPE" == "msys" ]]; then
# Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
elif [[ "$OSTYPE" == "win32" ]]; then
# I'm not sure this can happen.
elif [[ "$OSTYPE" == "freebsd"* ]]; then
else
# Unknown.
fi
31k questions
32.8k answers
501 comments
693 users