Bash sets the shell variable OSTYPE. From man bash
:
Automatically set to a string that describes the operating system on which bash is executing.
This has a tiny advantage over uname
in that it doesn't require launching a new process, so will be quicker to execute.
However, I'm unable to find an authoritative list of expected values. For me on Ubuntu 14.04 it is set to 'linux-gnu'. I've scraped the web for some other values. Hence:
case "$OSTYPE" in
linux*) echo "Linux / WSL" ;;
darwin*) echo "Mac OS" ;;
win*) echo "Windows" ;;
msys*) echo "MSYS / MinGW / Git Bash" ;;
cygwin*) echo "Cygwin" ;;
bsd*) echo "BSD" ;;
solaris*) echo "Solaris" ;;
*) echo "unknown: $OSTYPE" ;;
esac
The asterisks are important in some instances - for example OSX appends an OS version number after the 'darwin'. The 'win' value is actually 'win32', I'm told - maybe there is a 'win64'?
Perhaps we could work together to populate a table of verified values here:
linux-gnu
cygwin
msys
(Please append your value if it differs from existing entries)