views:

35

answers:

3

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?

+3  A: 

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

pax> uname -o
Cygwin

Unfortunately, I don't have a Linux or Mac handy so someone else will have to add those.

According to the very helpful schot, uname -s gives Darwin for OSX and Linux for Linux and my Cygwin gives CYGWIN_NT-5.1. But you'll probably have to experiment with all sorts of different versions.

paxdiablo
The `-o` option is nonstandard, did you mean `-s`? Results for OSX: "Darwin", Linux: "Linux" (for `-s`) and "GNU/Linux" (for `-o`).
schot
`-o` works under Cygwin. You must be using one of those "lesser" BSD variants :-) I've added `-s` output for Cygwin and I'll update it with your info. Thanks.
paxdiablo
Sometimes less is more ;) BTW, Wikipedia has a table of example uname output at http://en.wikipedia.org/wiki/Uname
schot
A: 

If uname, as suggested by paxdiablo, gives not enough information, you can check for existence of some specific files, e.g.,/Applications/iPhoto.app/ directory on Mac OS X.

mouviciel
And Cygwin tends to have a `/cygdrive` directory for accessing `c:` and others.
paxdiablo
+1  A: 

http://en.wikipedia.org/wiki/Uname

All the info you'll ever need. Google is your friend.

rubenvb