Bash – How to find out what version of RHEL I’m using

bashrhel

I work in an environment where we are slowly transitioning machines from Red Hat Enterprise Linux 5 to RHEL 6.

I need some of my scripts to do something subtly different on RHEL6 machines to what is currently being done on RHEL5 machines.

The pragmatic solution is to check at runtime and run some commands on RHEL5, others on RHEL6 and some on both.

A practical example of this is that we are using environment modules and my .bashrc includes a module load git line, but on RHEL6 machines this command errors:

RHEL6 system, git should be installed - not loading module

Looking in the modulefile I find the following code:

set redhatrelease [eval exec "cat /etc/redhat-release"]
if { [regexp -nocase {release 6} $redhatrelease] } {
  puts stderr "\n\t RHEL6 system, git should be installed - not loading module\n"
} else {
  ...
}

This seems to do what I want, but I was hoping for something shorter.

So, what is the easiest way to tell RHEL5 from RHEL6 in a bash script?

Ideally it should be robust across different major versions, but be tolerant of variations in minor release numbers.

Best Answer

You can also use the lsb_release command. If you're already certain it's RHEL, getting the major version number is:

majversion=$(lsb_release -rs | cut -f1 -d.)
Related Question