Notes

OSTYPE in Shell

checking $OSTYPE (and uname -s) to tell linux from macos in shell scripts, and how bash and zsh report darwin differently

Edit on GitHub

Bash Scripting

OSTYPE

OSTYPE variable stores the value of the operating system type.

1echo $OSTYPE
linux-gnu # Ubuntu

zsh and bash values differ a bit.

bash

$ echo $OSTYPE
  darwin18

zsh

% echo $OSTYPE
  darwin18.0

You can use a glob to account for the inconsistency

if [[ $OSTYPE == darwin* ]]; then
alias flushdns='sudo dscacheutil -flushcache'
fi

Another code snippet from the virualmin install script

 1remove_virtualmin_release () {
 2  # shellcheck disable=SC2154
 3  case "$os_type" in
 4    "fedora" | "centos" | "rhel" | "amazon"	)
 5    run_ok "rpm -e virtualmin-release" "Removing virtualmin-release"
 6    ;;
 7    "debian" | "ubuntu" )
 8    grep -v "virtualmin" /etc/apt/sources.list > "$tempdir"/sources.list
 9    mv "$tempdir"/sources.list /etc/apt/sources.list
10    ;;
11  esac
12}

uname -s

-s prints the kernel name

Linux # Ubuntu

Related

Browse all topics