Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.