7

How to find the operating system using bash script? I found this answer: Detect the OS from a Bash script. It is not clear it would work on Mac OS X.

I would like to find it on Mac OS X vs different linux OS's.

4

3 Answers 3

10

For Linux you can type in the following bash command:

$ cat /etc/*-release

For Mac OS X you can try one of these commands:

$ sw_vers -productVersion 
$ system_profiler SPSoftwareDataType
Sign up to request clarification or add additional context in comments.

1 Comment

I've found that the following command properly shows me the distro information on Linux just about on everything I tried: grep -Eh -- "DISTRIB_DESCRIPTION=|PRETTY_NAME=" /etc/*{_version,*-release} 2>/dev/null | cut -d= -f2 | tr -d "\"" | sort -u
7

Derived from other answers, this worked for me:

CURRENT_OS="OSX" #CENTOS, UBUNUTU are other valid options
function findCurrentOSType()
{
    echo "Finding the current os type"
    echo
    osType=$(uname)
    case "$osType" in
            "Darwin")
            {
                echo "Running on Mac OSX."
                CURRENT_OS="OSX"
            } ;;    
            "Linux")
            {
                # If available, use LSB to identify distribution
                if [ -f /etc/lsb-release -o -d /etc/lsb-release.d ]; then
                    DISTRO=$(gawk -F= '/^NAME/{print $2}' /etc/os-release)
                else
                    DISTRO=$(ls -d /etc/[A-Za-z]*[_-][rv]e[lr]* | grep -v "lsb" | cut -d'/' -f3 | cut -d'-' -f1 | cut -d'_' -f1)
                fi
                CURRENT_OS=$(echo $DISTRO | tr 'a-z' 'A-Z')
            } ;;
            *) 
            {
                echo "Unsupported OS, exiting"
                exit
            } ;;
    esac
}

Comments

5

use uname

$(uname -s)

this will give you the os name (Darwin = OSX)

$(uname -v)

will give you the os version

see uname manual here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.