If you do cat /etc/os-release you will find a line with almost exactly what you wish. Mine showsVERSION_ID=40
This is on the system, but I have no clue how to do the same to identify the latest released version on the web other than looking at the download page and pulling the data from there. The download page (for whatever spin) should always show the latest released version.
Fedora 40 is downloaded from https://download.fedoraproject.org/pub/fedora/linux/releases/40/
After a prompt (to extract the highest 2-digit number and export as environment variable)
ChatGPT gave me this, I have not tried or corrected it. The idea with the β/β was of the AI, I overlooked that.
#!/bin/bash
# Fetch the HTML content from the redirected mirror
url="https://download.fedoraproject.org/pub/fedora/linux/releases/"
html=$(curl -Ls "$url")
# Extract the highest two-digit number
highest_version=$(echo "$html" | grep -oE '[0-9]{2}/' | sed 's#/##' | sort -n | tail -1)
# Set the environment variable
export VERSION=$highest_version
echo "VERSION=$VERSION"
Explanation:
curl -Ls fetches the HTML content of the page.
grep -oE '[0-9]{2}/' extracts all two-digit numbers followed by a slash.