cat /etc/redhat-release


CentOS Linux release 5.6.1804 (Core)

how do I select the first number I've only tried

cat /etc/redhat-release | awk '{print $4}'

and this gives me back:


but I just want the first number

$ echo 'CentOS Linux release 5.6.1804 (Core)'|awk '{print $4}'|awk -F '.' '{print $1}'

Need I say more?

  • @ortiga Don't forget to select one answer you like the most and accept it. Personally, I would select the lsb_release answer, but my answer is closest to what you originally did. – juhist Jan 23 '19 at 20:59

If your CentOS machine has redhat-lsb or redhat-lsb-core installed:

lsb_release -sr | cut -d '.' -f 1

This would get the release version (only) using the lsb_release command and then cut out the bit before the first dot.

On a 16.04 Ubuntu machine:

$ lsb_release -sr | cut -d '.' -f 1

Using awk:

awk -F '[ .]' '{print $4}' /etc/redhat-release

Using sed:

sed -E 's/[ Aa-Zz]*([0-9]+).*/\1/' /etc/redhat-release

Using grep:

grep -Eo ' [0-9]' /etc/redhat-release
  • There's also /etc/os-release – Sammitch Jan 25 '19 at 0:28

There are so many ways, here is one of those:

cat /etc/redhat-release | cut -d " " -f4 | cut -d . -f1

The easier way would be using something that is prepared just for that:

[user@server]$ source /etc/os-release && echo $CENTOS_MANTISBT_PROJECT_VERSION

sed -Ee's/^.* ([0-9.]+) .*$/\1/' /etc/redhat-release |cut -f1,1 -d.

Sed command extracts the whole version number (same as your AWK, so you could use that as well) and the second extracts the first field from that using '.' as the separator.

so you could also use: awk '{print $4}' /etc/redhat-release |cut -f1,1 -d.


I would do something akin to this. On my RH7 box:

$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.5 (Maipo)

$ cut -f 7 -d " " /etc/redhat-release

$ cut -f 7 -d " " /etc/redhat-release | IFS=. read MAJOR MINOR RELEASE

7 5


Now you have all the components of the build in an env var to be referneced at you leisure. There are more sophisticated ways to get the original data, but I think that's beyond the scope of this question, which is just extracting the components of a dotted version string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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