pmod pmod -4 years ago 123
Linux Question

Identify Cygwin, Linux, Windows using environment variables

The problem arises when makefile needs be run on different OS and various setting should be properly set up (escaping, path separator, etc) depending on OS.
The first approach was to use Windows COMSPEC:

ifneq ($(COMSPEC)$(ComSpec),)
## in windows
## in linux

This is false positive for Cygwin, because it sees Windows' environment variables and detects Cygwin as Windows.
Then we tried Linux PWD:

ifeq ($(PWD),)
## in windows
## in linux, cygwin

However, as a result of integration of off-site tool we have PWD set in windows (one of the perl's modules). So, the detection fails again.

I wonder, what is the best approach to differentiate between Cygwin, Linux, Windows using environment variables?

Answer Source

Cygwin and (tested on Ubuntu) Linux provide an $OSTYPE environment variable, set to cygwin for Cygwin and linux-gnu for (Ubuntu) Linux.

Windows does not have this variable, and so it appears to be the only one you'll need. I suppose it's possible that your Linux doesn't provide it, in which case you can use $OSTYPE to distinguish between Windows and Cygwin and then fall back to uname for Cygwin vs. Linux.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download