5

I need to make script that behaves differently per system. Today it is possible to run bash even on microsoft windows, mac, linux, hp-ux, solaris etc...

How can I determine which of these operating systems I am on? I don't need exact version, I just need to know if I am on windows, linux, solaris...

1
  • yes very likely it is the same, but I will not delete this, just because I suppose some users would search for what I asked, I was googling this and couldn't find the linked answer Commented May 3, 2013 at 11:52

3 Answers 3

4

There is a standard shell command "uname" which returns the current platform as a string

To use this in a shell program a typical stanza might be

#!/bin/sh



if [ `uname` = "Linux" ] ;
then
    echo "we are on the operating system of Linux"
fi

if [ `uname` = "FreeBSD" ] ;
then
    echo "we are on the operating system of FreeBSD"
fi

More specific information is available but unfortunately it varies according to platform. On many versions of Linux ( and ISTR, Solaris ) there is a /etc/issue file which has the version name and number for the distribution installed. So on ubuntu

if [ -e "/etc/issue" ] ;
then
issue=`cat /etc/issue`
set -- $issue
if [ $1 = "Ubuntu" ] ;
then
    echo "we are on Ubuntu version " $2
fi
fi

This will give version information

Sign up to request clarification or add additional context in comments.

2 Comments

@ChetterHummin What is wrong with this post? It's just correct. Type uname in your terminal to see
@ChetterHummin added extensive additional information
0

I would look at the output of

uname -a

and look for specific strings, which can help you identify the system.

Or more specific

uname -s

With Windows, do you mean something like cygwin?

Comments

0

bash has a global var called $OSTYPE. Type echo $OSTYPE to see:

echo "$OSTYPE"
// linux-gnu

From the bash man page:

OSTYPE Automatically set to a string that describes the operating system on which bash is executing. The default is system-dependent.


An alternative is to use the uname command (without any arguments) or uname -s what it the same as uname defaults to -s.

Example on Linux

uname
// Linux

8 Comments

-o is not known on HP-UX as an example
Never logged into such box. I should do :) What would you suggest? -s ? This was what I had before. Wasn't sure
Yeah, -s gives you the OS. Plain 'uname' should be a shortcut to 'uname -s' but I guess it doesn't hurt to be explicit.
-s gives you the kernel not the OS. But I'm with you, it looks like this is what the OP is looking for
ok, than I would say -s , or no arguments, as its the default, is correct. isn't it?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.