Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Get Value of Centos Version and Assign It to a Variable

October 30, 2022
in Blog, Linux
0
ADVERTISEMENT

I’m writing a bash script, in the script, I want to download and install EPEL repo for CentOS, but as you can see, the repo links are difference for each OS version.

https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm

So, I want to get the value of CentOS version then assign it to a variable then my script can run in any CentOS version without specify link for each version.

# cat /etc/os-release
NAME="CentOS Stream"
VERSION="8"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"

# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"

# cat /etc/os-release
NAME="CentOS Stream"
VERSION="9"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="9"

To achieve that goal, I use the grep command as follows:

The excludes the VERSION_ID=” and the means to keep matching until found.

grep -oP 'VERSION_ID="\K[^"]+' /etc/os-release
# grep -oP 'VERSION_ID="\K[^"]+' /etc/os-release
8

Then I assign it to the variable $os_ver.

os_ver=$(. /etc/os-release; echo $VERSION_ID)
# os_ver=$(. /etc/os-release; echo $VERSION_ID)
# echo $os_ver
8

Finally, add it into the bash script to download the EPEL repo in CentOS.

os_ver=$(. /etc/os-release; echo $VERSION_ID)
echo $os_ver
sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-$os_ver.noarch.rpm

The output when I ran the script on CentOS 8.

# os_ver=$(. /etc/os-release; echo $VERSION_ID)
# echo $os_ver
8
# sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-$os_ver.noarch.rpm
Last metadata expiration check: 0:58:59 ago on Sat 29 Oct 2022 11:26:49 PM EDT.
epel-release-latest-8.noarch.rpm                                    25 kB/s |  24 kB     00:00
Package epel-release-8-17.el8.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!

The output when I ran the script on CentOS 7.

# os_ver=$(. /etc/os-release; echo $VERSION_ID)
# echo $os_ver
7
# sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-$os_ver.noarch.rpm
Loaded plugins: fastestmirror
epel-release-latest-7.noarch.rpm                                                     
Examining /var/tmp/yum-root-M_B6Zy/epel-release-latest-7.noarch.rpm: epel-release-7-14.noarch
Marking /var/tmp/yum-root-M_B6Zy/epel-release-latest-7.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-14 will be installed
--> Finished Dependency Resolution
...
Running transaction
  Installing : epel-release-7-14.noarch                                                 
  Verifying  : epel-release-7-14.noarch                                            

Installed:
  epel-release.noarch 0:7-14

Complete!
ADVERTISEMENT
5/5 - (1 vote)
Previous Post

How to Download and Install Latest cmake on CentOS Linux

Next Post

This bison Version is not Supported for Regeneration of the Zend/PHP

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory