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

bash check if windows
if macos bash
if test
shell check if osx
bash check if string in
bash test if string in variable
if else in linux
bash check if string in other string

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 detect whether it is running in Cygwin, on a Mac or in Linux?


Usually, uname with its various options will tell you what environment you're running in:

pax> uname -a
CYGWIN_NT-5.1 IBM-L3F3936 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin

pax> uname -s
CYGWIN_NT-5.1

And, according to the very helpful schot (in the comments), uname -s gives Darwin for OSX and Linux for Linux, while my Cygwin gives CYGWIN_NT-5.1. But you may have to experiment with all sorts of different versions.

So the bash code to do such a check would be along the lines of:

unameOut="$(uname -s)"
case "${unameOut}" in
    Linux*)     machine=Linux;;
    Darwin*)    machine=Mac;;
    CYGWIN*)    machine=Cygwin;;
    MINGW*)     machine=MinGw;;
    *)          machine="UNKNOWN:${unameOut}"
esac
echo ${machine}

Note that I'm assuming here that you're actually running within CygWin (the bash shell of it) so paths should already be correctly set up. As one commenter notes, you can run the bash program, passing the script, from cmd itself and this may result in the paths not being set up as needed.

If you are doing that, it's your responsibility to ensure the correct executables (i.e., the CygWin ones) are being called, possibly by modifying the path beforehand or fully specifying the executable locations (e.g., /c/cygwin/bin/uname).

bash - How to check if running in Cygwin, Mac or Linux?, Usually, uname with its various options will tell you what environment you're running in: pax> uname -a CYGWIN_NT-5.1 IBM-L3F3936 1.5.25(0.156/4/2)  Cygwin is a free command line interface that allows programs that were written for things like Linux and Unix to run on Windows. In other words, it provides an environment where Windows applications and tools can run alongside those from Unix or Linux and, for someone familiar with Unix environments,


How to check if running in Cygwin, Mac or Linux?, Windows/Cygwin and Mac and Linux. It needs slightly different variables for each versions. How can a shell/bash script detect whether it is running in Cygwin,​  j'ai un script shell qui est utilisé à la fois sur Windows/Cygwin et Mac et Linux. Il a besoin de variables légèrement différentes pour chaque version. comment un script shell/bash peut-il détecter S'il tourne sous Cygwin, Mac ou Linux?


Use uname -s (--kernel-name) because uname -o (--operating-system) is not supported on some Operating Systems such as Mac OS and Solaris. You may also use just uname without any argument since the default argument is -s (--kernel-name).

The below snippet does not require bash (i.e. does not require #!/bin/bash)

#!/bin/sh

case "$(uname -s)" in

   Darwin)
     echo 'Mac OS X'
     ;;

   Linux)
     echo 'Linux'
     ;;

   CYGWIN*|MINGW32*|MSYS*)
     echo 'MS Windows'
     ;;

   # Add here more strings to compare
   # See correspondence table at the bottom of this answer

   *)
     echo 'Other OS' 
     ;;
esac

The below Makefile is inspired from Git project (config.mak.uname).

ifdef MSVC     # Avoid the MingW/Cygwin sections
    uname_S := Windows
else                          # If uname not available => 'not' 
    uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
endif

# Avoid nesting "if .. else if .. else .. endif endif"
# because maintenance of matching if/else/endif is a pain

ifeq ($(uname_S),Windows)
    CC := cl 
endif
ifeq ($(uname_S),OSF1)
    CFLAGS += -D_OSF_SOURCE
endif
ifeq ($(uname_S),Linux)
    CFLAGS += -DNDEBUG
endif
ifeq ($(uname_S),GNU/kFreeBSD)
    CFLAGS += -D_BSD_ALLOC
endif
ifeq ($(uname_S),UnixWare)
    CFLAGS += -Wextra
endif
...

See also this complete answer about uname -s and Makefile.

The correspondence table in the bottom of this answer is from Wikipedia article about uname. Please contribute to keep it up-to-date (edit the answer or post a comment). You may also update the Wikipedia article and post a comment to notify me about your contribution ;-)

Operating System uname -s Mac OS X Darwin Cygwin 32-bit (Win-XP) CYGWIN_NT-5.1 Cygwin 32-bit (Win-7 32-bit)CYGWIN_NT-6.1 Cygwin 32-bit (Win-7 64-bit)CYGWIN_NT-6.1-WOW64 Cygwin 64-bit (Win-7 64-bit)CYGWIN_NT-6.1 MinGW (Windows 7 32-bit) MINGW32_NT-6.1 MinGW (Windows 10 64-bit) MINGW64_NT-10.0 Interix (Services for UNIX) Interix MSYS MSYS_NT-6.1 MSYS2 MSYS_NT-10.0-17763 Windows Subsystem for Linux Linux Android Linux coreutils Linux CentOS Linux Fedora Linux Gentoo Linux Red Hat Linux Linux Linux Mint Linux openSUSE Linux Ubuntu Linux Unity Linux Linux Manjaro Linux Linux OpenWRT r40420 Linux Debian (Linux) Linux Debian (GNU Hurd) GNU Debian (kFreeBSD) GNU/kFreeBSD FreeBSD FreeBSD NetBSD NetBSD DragonFlyBSD DragonFly Haiku Haiku NonStop NONSTOP_KERNEL QNX QNX ReliantUNIX ReliantUNIX-Y SINIX SINIX-Y Tru64 OSF1 Ultrix ULTRIX IRIX 32 bits IRIX IRIX 64 bits IRIX64 MINIX Minix Solaris SunOS UWIN (64-bit Windows 7) UWIN-W7 SYS$UNIX:SH on OpenVMS IS/WB z/OS USS OS/390 Cray sn5176 (SCO) OpenServer SCO_SV (SCO) System V SCO_SV (SCO) UnixWare UnixWare IBM AIX AIX IBM i with QSH OS400 HP-UX HP-UX

Bash - How to check if running in Cygwin Mac or Linux, 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  Check running process in Linux. The procedure to monitor the running process in Linux using the command line is as follows: Open the terminal window on Linux. For remote Linux server use the ssh command for log in purpose. Type the ps aux command to see all running process in Linux.


This is the bash shell to check which OS you are running on, based , I have a shell script that is used both on Windows/Cygwin and Mac and Linux. It needs slightly different variables for. You can use all of the basic Linux commands but if you want to get back to your C: drive you have to change directory to /cygdrive/c. To make Cygwin work in your normal Windows command prompt you need to add Cygwin to your Windows Environment Variables.


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 .

How can I tell whether my system is Unix or Linux?, to check which OS you are running on, based on stack overflow http://​stackoverflow.com/questions/3466166/how-to-check-if-running-in-cygwin-mac-or​-linux. Cygwin is free software to provide Linux and Unix environment, tools and libraries in Windows operating systems. Cygwin is also called an emulator because it translates Linux system calls into Windows type systems calls. Cygwin generally used to run Linux tools like ssh,scp,bash, X11,gcc in Windows environment.


In a bash script, how can I tell if the script is running under Ubuntu pure, POSIX defines uname ("Unix name") to provide information about the operating system and hardware platform; running uname gives the name of the  If your makefile may be running on non-Cygwin Windows, uname may not be available. That's awkward, but this is a potential solution. That's awkward, but this is a potential solution. You have to check for Cygwin first to rule it out, because it has WINDOWS in its PATH environment variable too.


Cygwin Installation, Check the current running kernel with uname -r . In WSL it will be suffixed by "-​Microsoft", ex: 4.4.0-17763-Microsoft (debian WSL). In Linux it  The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter .


Bash check if process is running or not on Linux / Unix, Run setup-x86.exe any time you want to update or install a Cygwin package for On Windows Vista and later, the setup program will check by default if it runs  scan all the running processes and check if the name is inside the list: bingo ! To scan all the processes: list every subdirectory in. If its name is digits, it is the pid of a running process. For example, the status of the process with pid 1234 is this file. /proc/1234/status. Open it and get the first line, starts with "Name:"