Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I know the commands to check the name of the Linux machine running on my machine. For example:


cat /etc/version


cat /etc/issue

How do I get the output from the terminal and compare to see if it is UBUNTU or CENTOS and perform the following commands?

apt-get install updates 


yum update
share|improve this question
Welcome to Ask Ubuntu. Please, could you put some of your time to read What should I do when someone answers my question? –  Sylvain Pineau May 6 '14 at 11:19

4 Answers 4

You don't need bash to do such task, and I'd suggest using a high-level approach to avoid dealing with files like /etc/version and /etc/issue (I don't have /etc/version on 13.10).

So my recommendation is to use this command instead:

python -mplatform | grep Ubuntu && sudo apt-get update || sudo yum update

python platform module will work on both systems, the rest of the command will check if Ubuntu is returned by python and run apt-get else yum.

share|improve this answer

Unfortunately, there is no surefire, simple way of getting the distribution name. Most major distros are moving towards a system where they use /etc/os-release to store this information. Most modern distributions also include the lsb_release tools but these are not always installed by default. So, here are some approaches you can use:

  1. Use /etc/os-release

    gawk -F= '/^NAME/{print $2}' /etc/os-release
  2. Use the lsb_release tools if available

    lsb_release -d | gawk -F"\t" '{print $2}'
  3. Use a more complex script that should work for the great majority of distros:

    # Determine OS platform
    UNAME=$(uname | tr "[:upper:]" "[:lower:]")
    # If Linux, try to determine specific distribution
    if [ "$UNAME" == "linux" ]; then
        # If available, use LSB to identify distribution
        if [ -f /etc/lsb-release -o -d /etc/lsb-release.d ]; then
            export DISTRO=$(lsb_release -i | cut -d: -f2 | sed s/'^\t'//)
        # Otherwise, use release info file
            export DISTRO=$(ls -d /etc/[A-Za-z]*[_-][rv]e[lr]* | grep -v "lsb" | cut -d'/' -f3 | cut -d'-' -f1 | cut -d'_' -f1)
    # For everything else (or if above failed), just use generic identifier
    [ "$DISTRO" == "" ] && export DISTRO=$UNAME
    unset UNAME
  4. Parse the version info of gcc if installed:

    CentOS 5.x

    $ gcc --version
    gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
    Copyright (C) 2006 Free Software Foundation, Inc.

    CentOS 6.x

    $ gcc --version
    gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)
    Copyright (C) 2010 Free Software Foundation, Inc.

    Ubuntu 12.04

    $ gcc --version
    gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
    Copyright (C) 2011 Free Software Foundation, Inc.

    Ubuntu 14.04

    $ gcc --version
    gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
    Copyright (C) 2013 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO

This has basically been directly copied from @slm's great answer to my question here.

share|improve this answer
@PeterMortensen please don't do such edits. You replaced the permanent links with ones that are likely to break. –  terdon May 7 '14 at 12:50

Use Chef for these tasks .;-)

In Chef, you can use the platform? method:

if platform?("redhat", "centos", "fedora")
  # Code for only Red Hat Linux family systems.


if platform?("ubuntu")
  # Code for only Ubuntu systems


if platform?("ubuntu")
  # Do Ubuntu things


if platform?("freebsd", "openbsd")
  # Do BSD things
share|improve this answer

The following script should tell if it is Ubuntu. If it is not and the only other option you have is CentOS, you should have it in an else clause:

dist=`grep DISTRIB_ID /etc/*-release | awk -F '=' '{print $2}'`

if [ "$dist" == "Ubuntu" ]; then
  echo "ubuntu"
  echo "not ubuntu"
share|improve this answer
This can probably be simplified using the lsb_release tool, which should read out the same files. lsb_release -i reports either Distributor ID: Ubuntu or Distributor ID: CentOS in these cases. –  chronitis May 2 '14 at 9:31
@chronitis: yes, sure it can be –  i08in May 2 '14 at 9:32
I don't think it's available by default on Centos though –  Sylvain Pineau May 2 '14 at 9:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.