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

后端 未结 11 894
忘掉有多难
忘掉有多难 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 01:01

    To build upon Albert's answer, I like to use $COMSPEC for detecting Windows:

    #!/bin/bash
    
    if [ "$(uname)" == "Darwin" ]
    then
     echo Do something under Mac OS X platform
    elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]
    then
      echo Do something under Linux platform
    elif [ -n "$COMSPEC" -a -x "$COMSPEC" ]
    then 
      echo $0: this script does not support Windows \:\(
    fi
    

    This avoids parsing variants of Windows names for $OS, and parsing variants of uname like MINGW, Cygwin, etc.

    Background: %COMSPEC% is a Windows environmental variable specifying the full path to the command processor (aka the Windows shell). The value of this variable is typically %SystemRoot%\system32\cmd.exe, which typically evaluates to C:\Windows\system32\cmd.exe .

提交回复
热议问题