-
-
Save virgilwashere/fef957068f3d34705ac3a5173975208d to your computer and use it in GitHub Desktop.
detect os in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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