Created
July 10, 2012 02:04
detect os in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
UNAME=$(uname) | |
if [ "$UNAME" == "Linux" ] ; then | |
echo "Linux" | |
elif [ "$UNAME" == "Darwin" ] ; then | |
echo "Darwin" | |
elif [[ "$UNAME" == CYGWIN* || "$UNAME" == MINGW* ]] ; then | |
echo "Windows" | |
fi |
Identify WSL
uname -r
will definately identify a WSL installation:
4.4.0-18362-Microsoft
OS detection script
And that script needs some love. ❤️
#!/usr/bin/env bash
UNAME=$( command -v uname)
case $( "${UNAME}" | tr '[:upper:]' '[:lower:]') in
linux*)
printf 'linux\n'
;;
darwin*)
printf 'darwin\n'
;;
msys*|cygwin*|mingw*)
# or possible 'bash on windows'
printf 'windows\n'
;;
nt|win*)
printf 'windows\n'
;;
*)
printf 'unknown\n'
;;
esac
uname output
🏁 Windows CMD
C:\> ver
Microsoft Windows [Version 10.0.18362.10000]
C:\> uname --version
GNU shellutils 1.9.4
C:\> uname --help
Usage: uname [OPTION]...
-a, --all print all information
-m, --machine print the machine (hardware) type
-n, --nodename print the machine's network node hostname
-r, --release print the operating system release
-s, --sysname print the operating system name
-v print the operating system version
C:\> uname -s
WindowsNT
C:\> uname -r
2
C:\> uname -v
6
WSL ⭕ Ubuntu 18.04
$ uname --version
uname (GNU coreutils) 8.28
$ uname --help
Usage: uname [OPTION]...
Print certain system information. With no OPTION, same as -s.
-a, --all print all information, in the following order,
except omit -p and -i if unknown:
-s, --kernel-name print the kernel name
-n, --nodename print the network node hostname
-r, --kernel-release print the kernel release
-v, --kernel-version print the kernel version
$ uname -s
Linux
$ uname -r
4.4.0-18362-Microsoft
$ uname -v
#10000-Microsoft Fri Jun 21 11:23:00 PST 2019
👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you know how this could be adapted to distinguish between running Linux and running WSL? WSL results in a linux output because it is technically Linux afterall.