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?
Try using "uname". For example, in Linux: "uname -a".
According to the manual page, uname conforms to SVr4 and POSIX, so it should be available on Mac OS X and Cygwin too, but I can't confirm that.
BTW: $OSTYPE is also set to
linux-gnu
here :)The bash manpage says that the variable OSTYPE stores the name of the operation system:
It is set to
linux-gnu
here.Doing the following helped perform the check correctly for ubuntu:
This should be safe to use on all distros.
This produces something like this.
Extract/assign to variables as you wish
Note: On some setups. This may also give you some errors that you can ignore.
You can use the following:
then you can use OS variable in your script.
For my .bashrc, I use the following code:
Then I do somethings like:
It's ugly, but it works. You may use
case
instead ofif
if you prefer.