I would like to keep my .bashrc
and .bash_login
files in version control so that I can use them between all the computers I use. The problem is I have some OS specific aliases so I was looking for a way to determine if the script is running on Mac OS X, Linux or Cygwin.
What is the proper way to detect the operating system in a Bash script?
$OSTYPE
You can simply use pre-defined
$OSTYPE
variable e.g.:However it's not recognized by the older shells (such as Bourne shell).
uname
Another method is to detect platform based on
uname
command.See the following script (ready to include in .bashrc):
You can find some practical example in my
.bashrc
.Here is similar version used on Travis CI:
You can use following if clause and expand it as needed:
try this:
I recommend to use this complete bash code
more examples examples here: https://github.com/coto/server-easy-install/blob/master/lib/core.sh
In bash, use
$OSTYPE
and$HOSTTYPE
, as documented; this is what I do. If that is not enough, and if evenuname
oruname -a
(or other appropriate options) does not give enough information, there’s always the config.guess script from the GNU project, made exactly for this purpose.I wrote these sugars in my
.bashrc
:So I can do stuff like: