Linux - Bash - Get $releasever and $basearch values?
Categories:
Dynamically Retrieving $releasever and $basearch in Linux Bash

Learn how to programmatically obtain the distribution release version ($releasever) and base architecture ($basearch) in Linux Bash scripts, crucial for building robust and portable system management tools.
When managing Linux systems, especially those using RPM-based distributions like CentOS, Fedora, or RHEL, you often encounter variables like $releasever and $basearch. These variables are fundamental for tasks such as configuring YUM/DNF repositories, installing architecture-specific packages, or tailoring scripts to different distribution versions. While YUM/DNF automatically resolves these, knowing how to retrieve them within a Bash script is essential for creating dynamic and adaptable automation.
Understanding $releasever and $basearch
Before diving into retrieval methods, let's clarify what these variables represent:
$releasever: This variable typically refers to the major release version of your operating system. For example, on CentOS 7, it would be7; on Fedora 38, it would be38. It's crucial for ensuring that your system pulls packages compatible with its specific distribution version.$basearch: This variable represents the base architecture of your system. Common values includex86_64for 64-bit Intel/AMD systems,aarch64for 64-bit ARM systems, orppc64lefor PowerPC Little Endian. Using$basearchensures that you download and install packages compiled for your system's specific hardware architecture.
flowchart TD
A[Start Bash Script] --> B{Need $releasever or $basearch?}
B -- Yes --> C[Identify Distribution (e.g., RHEL, Fedora)]
C --> D{Method for $releasever?}
D -- `rpm -E` --> E["Retrieve $releasever (e.g., `%{rhel}`)"]
D -- `/etc/os-release` --> F["Retrieve $releasever (e.g., `VERSION_ID`)"]
C --> G{Method for $basearch?}
G -- `uname -i` --> H["Retrieve $basearch (e.g., `x86_64`)"]
G -- `arch` --> I["Retrieve $basearch (e.g., `x86_64`)"]
E --> J[Use in Script]
F --> J
H --> J
I --> J
J --> K[End Bash Script]Decision flow for retrieving system variables in Bash
Retrieving $releasever
There are several reliable ways to get the $releasever value, depending on the distribution and the level of detail you need. The rpm -E command is often the most direct for RPM-based systems, as it mimics how YUM/DNF resolve these macros.
# Method 1: Using rpm -E (Recommended for RPM-based systems)
RELEASEVER_RPM=$(rpm -E %{rhel} || rpm -E %{fedora} || rpm -E %{centos} || rpm -E %{suse_version})
echo "Releasever (rpm -E): $RELEASEVER_RPM"
# Method 2: Using /etc/os-release (More generic, but might need parsing)
if [ -f /etc/os-release ]; then
. /etc/os-release
RELEASEVER_OS_RELEASE="$VERSION_ID"
echo "Releasever (/etc/os-release): $RELEASEVER_OS_RELEASE"
fi
# Method 3: Using lsb_release (If available, less common on minimal installs)
if command -v lsb_release &> /dev/null; then
RELEASEVER_LSB=$(lsb_release -rs)
echo "Releasever (lsb_release): $RELEASEVER_LSB"
fi
Different methods to retrieve the $releasever value.
rpm -E %{rhel} command is particularly useful on RHEL and its derivatives (CentOS, Rocky Linux, AlmaLinux) as it directly provides the major release number. For Fedora, use rpm -E %{fedora}.Retrieving $basearch
The $basearch value is generally easier to obtain as it directly relates to the system's CPU architecture. The uname -i or arch commands are the most common and reliable ways.
# Method 1: Using uname -i (Most common and reliable)
BASEARCH_UNAME=$(uname -i)
echo "Basearch (uname -i): $BASEARCH_UNAME"
# Method 2: Using arch command (Often a symbolic link to uname -m or similar)
BASEARCH_ARCH=$(arch)
echo "Basearch (arch): $BASEARCH_ARCH"
# Method 3: Using rpm --eval (If you want to mimic YUM/DNF's exact resolution)
BASEARCH_RPM=$(rpm --eval %{_arch})
echo "Basearch (rpm --eval): $BASEARCH_RPM"
Different methods to retrieve the $basearch value.
uname -m also provides architecture information (e.g., x86_64), uname -i is often preferred for $basearch as it specifically reports the hardware platform name, which aligns more closely with how package managers categorize architectures.Practical Application: Dynamic Repository Configuration
Combining these techniques allows you to create highly flexible scripts. For instance, you can dynamically generate a YUM/DNF repository file that adapts to the host system's release and architecture.
#!/bin/bash
# Get releasever
RELEASEVER=$(rpm -E %{rhel} || rpm -E %{fedora} || rpm -E %{centos} || rpm -E %{suse_version})
if [ -z "$RELEASEVER" ]; then
echo "Error: Could not determine releasever."
exit 1
fi
# Get basearch
BASEARCH=$(uname -i)
if [ -z "$BASEARCH" ]; then
echo "Error: Could not determine basearch."
exit 1
fi
REPO_FILE="/etc/yum.repos.d/my-custom.repo"
cat <<EOF > "$REPO_FILE"
[my-custom-repo]
name=My Custom Repository for RHEL/CentOS $RELEASEVER - $BASEARCH
baseurl=http://example.com/repos/$RELEASEVER/$BASEARCH/
enabled=1
gpgcheck=0
EOF
echo "Generated repository file: $REPO_FILE"
cat "$REPO_FILE"
# Example of using the variables directly
echo "\nSystem is running $RELEASEVER on $BASEARCH architecture."
Bash script to dynamically create a YUM/DNF repository file.
This script first attempts to determine $releasever using rpm -E for various distributions. If that fails, you might add fallback logic using /etc/os-release. It then gets $basearch using uname -i. Finally, it uses these variables to construct a .repo file, ensuring that the baseurl points to the correct path for the specific system.
.repo files) in a non-production environment first. Ensure your baseurl paths are correct and accessible.