11

I am writing a shell script in which I need the current operating system name to make it generic. Like:

if [ $Operating_System == "CentOS" ]
then
    echo "CentOS";
    # Do this
elif [ $Operating_System == "Ubuntu" ]
then
    echo "Ubuntu";
    # Do that
else
    echo "Unsupported Operating System";
fi

How will it be possible? Applying regular expression on lsb_release -a command or something else?

Thanks..

3
  • Yup - lsb-release is definitely the most robust, generic way to do it. EXAMPLE: lsb_release -d|awk '{print $2}'. Commented Apr 11, 2015 at 18:58
  • or lsb_release -ds Commented Apr 11, 2015 at 19:13
  • lsb_release seems not available on CENTOS7 out of the box. Commented Aug 24, 2017 at 7:58

7 Answers 7

10
$ lsb_release -i
Distributor ID: Fedora
$ lsb_release -i | cut -f 2-
Fedora
Sign up to request clarification or add additional context in comments.

1 Comment

But, lsb_release not exists on centos 7.
8

You can get the info from lsb_release:

echo "$(lsb_release -is)"

i stands for distributor id.

s stands for short.

For ex. It shows Ubuntu instead of Distributor Id: Ubuntu

There are other options:

-r : release

-d : description

-c : codename

-a : all

You can get this information by running lsb_release --help or man lsb_release

Comments

4

EDIT 2: As pointed out by @Jean-Michaël Celerier, some distros prefer to add double quotes.

This example...

awk -F'=' '/^ID=/ { gsub("\"","",$2); print tolower($2) }' /etc/*-release 2> /dev/null

... and this one ...

(awk -F'=' '/^ID=/ { print tolower($2) }' /etc/*-release | tr -d '"') 2> /dev/null

... can be used to remove them.

EDIT 1: as suggested by @S0AndS0, this is slightly better:

awk -F'=' '/^ID=/ {print tolower($2)}' /etc/*-release 2> /dev/null

try this one:

awk '/^ID=/' /etc/*-release | awk -F'=' '{ print tolower($2) }'

2 Comments

This is almost perfect, no need for the piping between awk though as awk -F'=' '/^ID=/ {print tolower($2)}' /etc/*-release does just fine, also for those with the want to detect distros based on Debian awk -F'=' '/^ID_LIKE=/ {print tolower($2)}' /etc/*-release 2>/dev/null is even closer to perfect for my use case.
Small improvement: remove the quotes that some distros like opensuse add: awk -F'=' '/^ID=/ { gsub("\"","",$2); print tolower($2) } ' /etc/*-release 2> /dev/null
3
DISTRO=$( cat /etc/*-release | tr [:upper:] [:lower:] | grep -Poi '(debian|ubuntu|red hat|centos|nameyourdistro)' | uniq )
if [ -z $DISTRO ]; then
    DISTRO='unknown'
fi
echo "Detected Linux distribution: $DISTRO"

Comments

1

For almost all linux distros, cat /etc/issue will do the trick.

Edit: Obviously, no solution can apply to all distros, as distros are free to do as they please.

Further clarification: This is not guaranteed to work - nothing is - but in my experience, this is the method that most often works. Actually, it's the only method that works consistently (lsb_release, which was mentioned here, often produces command not found).

4 Comments

/etc/issue is not meant for storing information about the operating system and may contain whatever text the admin likes. I would never trust it for detecting operating system or distribution.
Specifically, /etc/issue is a text file displayed by getty at the console login. It is not guaranteed to contain any meaningful information at all about the distribution.
Normally I do cat /etc/*ease, since there is a fedora-release or something-release file in /etc.
this is not a reliable solution, since these are files for the administrator to put in whatever he wants.
0

Here is what i get:

#!/bin/bash

dist=$(tr -s ' \011' '\012' < /etc/issue | head -n 1)

check_arch=$(uname -m)

echo "[$green+$txtrst] Distribution Name: $dist"

Comments

-3

I'd use uname -a

robert@debian:/tmp$ uname -a
Linux debian 3.2.0-4-686-pae #1 SMP Debian 3.2.65-1+deb7u2 i686 GNU/Linux

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.