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...

Petr
  • 13,747
  • 20
  • 89
  • 144
  • 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 – Petr May 03 '13 at 11:52

3 Answers3

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

Vorsprung
  • 32,923
  • 5
  • 39
  • 63
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?

Devolus
  • 21,661
  • 13
  • 66
  • 113
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
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • -o is not known on HP-UX as an example – Devolus May 03 '13 at 09:50
  • Never logged into such box. I should do :) What would you suggest? `-s` ? This was what I had before. Wasn't sure – hek2mgl May 03 '13 at 09:52
  • 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. – Devolus May 03 '13 at 09:54
  • -s gives you the `kernel` not the OS. But I'm with you, it looks like this is what the OP is looking for – hek2mgl May 03 '13 at 09:55
  • You are right. What does "kernel" mean though? On my HPUX box it plainly says that it will give the OS name, while on the linux manpage it's talking about the kernel name. Not sure if the kernel name is just somehting arbitrary or if it includes the OS implicitly. At the moment I don't have access to a linux box so I can't check. – Devolus May 03 '13 at 10:01
  • What does uname -s return on your box? – hek2mgl May 03 '13 at 10:07
  • HP-UX on the HP-UX machine and Cygwin on the cygwin terminal. – Devolus May 03 '13 at 10:09
  • 1
    ok, than I would say `-s` , or no arguments, as its the default, is correct. isn't it? – hek2mgl May 03 '13 at 10:11