Skip to content

Instantly share code, notes, and snippets.

@lyndell
Last active May 16, 2023 04:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyndell/8c5ef8ff9eba2098717f to your computer and use it in GitHub Desktop.
Save lyndell/8c5ef8ff9eba2098717f to your computer and use it in GitHub Desktop.
test for the OS with the shell environment variable $OSTYPE
#!/bin/bash -x
#
# From: http://stackoverflow.com/questions/394230/detect-the-os-from-a-bash-script
PLATFORM=$(uname)
case "$PLATFORM" in
"Darwin")
# macOS
;;
"Linux")
# Linux distro,
;;
esac
case "$OSTYPE" in
solaris*) echo "SOLARIS" ;;
darwin*) echo "OSX" ;;
linux*) echo "LINUX" ;;
bsd*) echo "BSD" ;;
*) echo "unknown: $OSTYPE" ;;
esac
if [[ "$OSTYPE" == "linux-gnu" ]]; then
# ...
echo "linux-gnu"
elif [[ "$OSTYPE" == "darwin"* ]]; then
# ...
echo "darwin"
elif [[ "$OSTYPE" == "cygwin" ]]; then
# ...
echo "cygwin"
elif [[ "$OSTYPE" == "win32" ]]; then
# ...
echo "win32"
elif [[ "$OSTYPE" == "freebsd" ]]; then
# ...
echo "freebsd"
else
# Unknown.
echo "Wow, that's a weird one.\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment