Skip to content

Instantly share code, notes, and snippets.

@marcusandre
Last active January 31, 2019 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcusandre/4b88c2428220ea255b83 to your computer and use it in GitHub Desktop.
Save marcusandre/4b88c2428220ea255b83 to your computer and use it in GitHub Desktop.
Get operating system as string identifier from $OSTYPE
#!/bin/sh
#
# get os type
#
get_os() {
if [ -z $OSTYPE ]; then
OSTYPE=$(uname | tr '[:upper:]' '[:lower:]')
fi
case $OSTYPE in
freebsd*) echo "freebsd" ;;
netbsd*) echo "netbsd" ;;
openbsd*) echo "openbsd" ;;
darwin*) echo "mac" ;;
linux*) echo "linux" ;;
cygwin*) echo "cygwin" ;;
*) echo "unknown"; exit 1 ;;
esac
}
@marcusandre
Copy link
Author

edit: add OSTYPE fallback for non-posix shells.

@marcusandre
Copy link
Author

edit: add bsd types.

@dewyatt
Copy link

dewyatt commented Jan 31, 2019

This will fail when OSTYPE is set to something that isn't all lowercase (FreeBSD).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment