How to check the RHEL version in Linux shell and store it in a variable

How can I check the RHEL version in Linux shell? What I need is to store it in a shell variable called MY_OS. How can it be done?

Run the following command to display RHEL or CentOS 8 version

cat /etc/redhat-release

Here is outputs from my CentOS 8 server:

CentOS Linux release 8.2.2004 (Core)

Another option to check the RHEL version

cat  /etc/os-release

Use the grep command:

grep VERSION_ID /etc/os-release
grep PRETTY_NAME /etc/os-release
MY_OS="$(grep PRETTY_NAME /etc/os-release)"
MY_OS_VERSION="$(grep VERSION_ID /etc/os-release)"
echo "$MY_OS"

See my page

1 Like

Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki