Niubbo75
Niubbo75

Reputation: 11

How to show in bash script the release of a Red Hat distribution

I have a bash script where I request to add a repository related to the current release I'm running, I have define my variable this way:

RedHat_VER=`rpm -qi --whatprovides /etc/redhat-release | awk '/Version/ {print $3}'`

but in this way I get, i.e. "7.6" but I need to get "7", how can I do that?

Thanks to all who can help me. Best regards,

Alessandro.

Upvotes: 0

Views: 1630

Answers (4)

Cyrus
Cyrus

Reputation: 88674

With RHEL7:

. /etc/os-release
echo "${VERSION_ID%.*}"

Output:

7

Upvotes: 2

nullPointer
nullPointer

Reputation: 4574

Here's one way :

rpm -qia '*release*' | grep Version | cut -d':' -f2 | tr -d ' ' | cut -d'.' -f1

Another way :

cat /etc/redhat-release | cut -d'.' -f1 | awk '{print $NF}'

Upvotes: 0

kvantour
kvantour

Reputation: 26481

$ lsb_release -rs | cut -d. -f1

Upvotes: 0

accdias
accdias

Reputation: 5372

You can use lsb_release -r. The output will be something like this, which is easily parsable:

$ lsb_release -r
Release:        6.10
$ 

And you can make it easier by using -s (check man lsb_release):

$ lsb_release -rs
6.10
$ 

Upvotes: 0

Related Questions