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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Usually,
unamewith its various options will tell you what environment you’re running in:And, according to the very helpful
schot(in the comments),uname -sgivesDarwinfor OSX andLinuxfor Linux, while my Cygwin givesCYGWIN_NT-5.1. But you may have to experiment with all sorts of different versions.So the
bashcode to do such a check would be along the lines of:Note that I’m assuming here that you’re actually running within CygWin (the
bashshell of it) so paths should already be correctly set up. As one commenter notes, you can run thebashprogram, passing the script, fromcmditself 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).