Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

I need to collect some system info from machines and decide what to do based on some distribution/version -specific conditionals.

To simplify my example, I concocted a fictitious my_function_display_operating_system function. It is a useless function but it frames the problem:

  1. detect the general OS type (, , etc.)
  2. detect some version information about the OS
  3. detect the distribution friendly name

Point 3. is what I most need help with, please.

Is there a reliable way to read a string indicating the host distribution name?

I am using /etc/issue in the example below as I know of no other way, or set of ways, to get to this information.

I am happy with a it depends answer, I can build a quite involved case esac switch statement if need be.

I just need to know where to look, and I can't download/install VMs for all the distros I know of to "look around" and/or for the name..


# Displays operating system.

function my_function_display_operating_system() {
  local operating_system
  case $my_global_os_type in
    OSX)
      printf -v operating_system \
                'OS X v%s (build %s)' \
                $(sw_vers -productVersion) \
                $(sw_vers -buildVersion)
      ;;
    Linux)
      local -r linux_kernel_version=$(uname -r)
      printf -v operating_system \
                'GNU/Linux (kernel %s)' \
                "$linux_kernel_version"
      if [[ -f /etc/issue ]]; then
        local -r distribution=$(cat /etc/issue | sed 's/ \\n \\l//g')
        operating_system+="\nDistribution: $distribution"
      fi
      ;;
    *)
      operating_system=Unknown
      ;;
  esac
  printf "Operating system: $operating_system\n"
}

Sample output:


> my_function_display_system_information
Operating system   GNU/Linux (kernel 3.5.0-25-generic)
Distribution       Ubuntu 12.10
System memory      1 GB
Bash               v4.2.37(1)-release
Vim                VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Oct 26 2012 16  45  54)
# ... etc ...

share|improve this question

marked as duplicate by Renan, jasonwryan, jordanm, uther, derobert Apr 2 '13 at 22:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Yes! It's a dup! –  Robottinosino Apr 2 '13 at 22:35

1 Answer 1

Very similar advice given here:

http://superuser.com/questions/11008/how-do-i-find-out-what-version-of-linux-im-running

The short answer:

 ls /etc/*{release,version}

Various distributions will spit out different formats, unfortunately there's no single uniform system. The answer above lists much more specifically some of the most common files to look for.

share|improve this answer

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