📝 Edit page
➕ Add page
Check OS
Get the name of the current operating using the shell and use it in control flow
Display
The OS type variable
echo "$OSTYPE"
OS | Example value |
---|---|
Linux | linux-gnu |
macOS | darwin19.0 |
The uname command
See uname cheatsheet.
$ uname
e.g.
OS | Example uname value |
---|---|
Linux | Linux |
macOS | Darwin |
Using uname
in a conditional using contains and -s
flag. I’ve seen this approach used before.
if [[ "$(uname -s)" =~ 'Linux' ]]; then
echo 'Linux!'
fi
Set boolean
This was based on a Grails quickstart app which uses case
and uname
.
IS_MAC=false
IS_LINUX=false
case "$(uname)" in
Darwin* )
IS_MAC=true
;;
Linux* )
IS_LINUX=true
;;
esac
Example use:
if [[ $IS_MAC == 'true' ]]; then
echo 'Hello, macOS!'
fi
Inline control flow
Perform a check inline to produce a string. Note this could be improved by first storing a value.
$ echo "Hello $([[ "$OSTYPE" == darwin* ]] && echo 'Mac')"
- Result if Mac:
Hello Mac
- Result if not Mac:
Hello
Release
$ less /etc/*release
Example for Linux Lite 5, which uses Debian and Ubuntu:
lsb-release
DISTRIB_ID=Ubuntu DISTRIB_RELEASE=20.04 DISTRIB_CODENAME=focal DISTRIB_DESCRIPTION="Linux Lite 5.0"
os-release
NAME="Ubuntu" VERSION="20.04 LTS (Focal Fossa)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Linux Lite 5.0" VERSION_ID="20.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launc hpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=focal UBUNTU_CODENAME=focal
On an AWS EC2 instance:
/etc/os-release
NAME="Amazon Linux" VERSION="2" ID="amzn" ID_LIKE="centos rhel fedora" VERSION_ID="2" PRETTY_NAME="Amazon Linux 2" ANSI_COLOR="0;33" CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2" HOME_URL="https://amazonlinux.com/"
/etc/system-release
Amazon Linux release 2 (Karoo)
Check OS flavor
Check if Linux Lite
Linux Lite uses a text file which includes the version.
IS_LINUX_LITE=$([[ -f /etc/llver ]] && echo 'true' || echo 'false')
if [[ -f /etc/llver ]]; then
IS_LINUX_LITE=true
else
IS_LINUX_LITE=false
fi