How to check if running in Cygwin, Mac or Linux?

后端 未结 11 832
忘掉有多难
忘掉有多难 2020-11-27 23:58

I have a shell script that is used both on Windows/Cygwin and Mac and Linux. It needs slightly different variables for each versions.

How can a shell/bash script de

11条回答
  •  有刺的猬
    2020-11-28 00:57

    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 Ubuntu (incl. WSL): linux-gnu
    • Cygwin 64-bit: cygwin
    • Msys/MINGW (Git Bash for Windows): msys

    (Please append your value if it differs from existing entries)

提交回复
热议问题