Snippets

Ben Buchanan BASH: operating system detect including WSL distro

Created by Ben Buchanan
#!/bin/bash

# where are we...
case "$(uname -s)" in
  CYGWIN*)    OS="cygwin" ;;
  Darwin*)    OS="osx" ;;
  Linux*)     OS="linux" ;;
  MINGW32*)   OS="windows" ;;
  *)          echo "OS not detected successfully" ;;
esac

# check for WSL (bash-on-ubuntu-on-windows)
# Note this doesn't differentiate distros
if [[ $OS = "linux" ]]; then
  if $(grep --silent 'Microsoft' /proc/sys/kernel/osrelease); then
    OS="wsl"
  fi
fi

# ...but this does differentiate WSL distros
if [[ $OS = "wsl" ]]; then
  DISTRONAME=$(cat /etc/*-release | grep ^NAME)
  if [[ $DISTRONAME == *"Ubuntu"* ]]; then
    WSLDISTRO="ubuntu"
  fi
  if [[ $DISTRONAME == *"openSUSE"* ]]; then
    WSLDISTRO="opensuse"
  fi
fi

echo -e "OS: $OS"
echo -e "WSLDISTRO: $WSLDISTRO"

# USAGE

# if [[ $OS == 'osx' ]];then
# else
# fi

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.