Skip to content

Instantly share code, notes, and snippets.

@virgilwashere
Forked from prabirshrestha/detect-os.sh
Last active July 25, 2022 21:04
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 virgilwashere/fef957068f3d34705ac3a5173975208d to your computer and use it in GitHub Desktop.
Save virgilwashere/fef957068f3d34705ac3a5173975208d to your computer and use it in GitHub Desktop.
detect os in bash
#!/usr/bin/env bash
# "unofficial" bash strict mode
# See: http://redsymbol.net/articles/unofficial-bash-strict-mode
set -o errexit # Exit when simple command fails 'set -e'
set -o errtrace # Exit on error inside any functions or subshells.
set -o nounset # Trigger error when expanding unset variables 'set -u'
set -o pipefail # Do not hide errors within pipes 'set -o pipefail'
IFS=$'\n\t'
# do nothing if the library is already sourced in the same shell
((${__sourced__-})) && return
( return 0 2>/dev/null) && __sourced__=1 || __sourced__=0
os_name() {
UNAME=$( command -v uname)
TR=$( command -v tr)
case $( "${UNAME}" | "${TR}" '[:upper:]' '[:lower:]') in
linux*) printf 'linux\n' ;;
darwin*) printf 'osx\n' ;;
nt|win*) printf 'windows\n' ;;
msys*|cygwin*|mingw*) printf 'windows\n' ;; # or possibly 'bash on windows'
*) printf 'unknown\n' ;;
esac
unset -v UNAME
true
}
if ((${__sourced__-})); then
export -f os_name
else
os_name
return $? 2>/dev/null || exit $?
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment