os#
OS detection, shell capability probes and conditional sudo.
Functions#
- get_os
- Purpose: Detect the current OS.
- Returns:
linux,mac,windows, orunknownbased on$OSTYPE. -
Notes: matches any
linux*- bash reportslinux-muslon Alpine andlinux-androidon Termux, and both are Linux. Reads$OSTYPEdefensively, so a caller running underset -ugetsunknownrather than an abort. -
getos
-
Purpose: Alias of
get_os(compatibility). -
is_macos
- Purpose: Test for macOS.
-
Returns: success when
get_osismac. -
is_linux
- Purpose: Test for Linux (glibc, musl or Android).
-
Returns: success when
get_osislinux. -
bash_major
- Purpose: Print the major version of the running bash (
3,5, ...). -
Returns:
${BASH_VERSINFO[0]}, or0when unset. -
bash_at_least major [minor]
- Purpose: Test the running bash against a minimum version.
- Args:
- major - required major version.
- minor - optional minimum minor version (default
0).
- Returns: success when the running bash is at least that version.
-
Example:
bash_at_least 4 || echo "no associative arrays here" -
require_bash4 feature
- Purpose: Fail loudly, with an actionable message, when the running bash
predates 4.0. On macOS the message names
brew install bash. - Args:
- feature - what needs bash 4, named in the error.
- Returns:
0on bash 4+,1otherwise (logging throughlog_errorwhen the logging module is loaded, else to stderr). -
Notes: reserved for the few entry points that genuinely cannot work on bash 3.2 - those taking an associative array from the caller, namely
select_distro,select_multiple_distrosanddownload_iso. Everything else in this library runs unchanged on bash 3.2, which is what macOS ships as/bin/bash; using this as a general guard would produce errors a caller cannot act on. -
is_wsl
- Purpose: Detect whether script is running in WSL/WSL2.
-
Returns: success when
/proc/versionincludes Microsoft orWSL_DISTRO_NAMEis set. -
run_with_optional_sudo use_sudo_bool command [args...]
- Purpose: Run a command with
sudowhen the first argument equalstrueandsudoexists. - Args:
- use_sudo_bool -
trueto use sudo if available, anything else to run directly. - command [args...] - command to execute.
- use_sudo_bool -
- Returns: exit code of the underlying command.