Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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 -qi 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
add -i to grep may help. – coanor Mar 28 at 3:12
@coanor: Good idea, thanks. – Sylvain Pineau Mar 28 at 8:00

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

Here's a simple answer that I find works across all versions of Ubuntu / CentOS / RHEL by the mere presence of the files (not failsafe of course if someone is randomly dropping /etc/redhat-release on your Ubuntu boxes, etc):

if [ -f /etc/redhat-release ]; then
  yum update

if [ -f /etc/lsb-release ]; then
  apt-get update
share|improve this answer
 apt-get -v &> /dev/null && apt-get update
 which yum &> /dev/null && yum update

if there are only two distro, then you can make it shorter:

apt-get -v &> /dev/null && apt-get update || yum update

somehow yum -v return non-zero in CentOS so use which instead,
of course you should consider scenario if there is no which installed.

share|improve this answer
which yum
if [[ $? -eq 0 ]]
    sudo yum update
    which apt-get
    if [[ $? -eq 0 ]]
        sudo apt-get update

if [[ -z $os ]]
    echo "Something is wrong with your os"
share|improve this answer
yum can be installed in Ubuntu and apt-get can be installed in CentOs too .. so nice code but wrong answer. – Ahmed Jerbi Jun 3 at 20:00
Since the point of the asker's question was to see if they should run the update using yum or apt-get, I disagree. – Sohrab T Jun 3 at 21:06

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.