-1

I want to store the value of os-version from the command cat /etc/os-release. The output of cat /etc/os-release is

VERSION="4.4"
ID="someId"
ID_LIKE="someIdLike"

I want a shell command to store the version "4.4" into a variable without apostrophes or the text "VERSION". So far what I have done is

variable==`grep 'VERSION' /etc/os-release

which gives me variable=" VERSION="4.4"" and I just want the numerical value of the version which would be just 4.4.

G Man
  • 1
  • 1

3 Answers3

2

The os-release manual on Ubuntu states that the /etc/os-release file should contain simple variable assignments compatible with the shell and that one should be able to source the file:

The basic file format of os-release is a newline-separated list of environment-like shell-compatible variable assignments. It is possible to source the configuration from shell scripts [...]

$ ( . /etc/os-release && printf '%s\n' "$VERSION" )
20.04.3 LTS (Focal Fossa)

Above, I'm sourcing the file in a subshell to avoid polluting my interactive environment. In a shell script, you might not care too much about that. After sourcing the file, I print the value of the VERSION variable.

The bare version number, without the patch release number, is available in the VERSION_ID variable on my system. Still, you could get it with the patch release included from the VERSION variable by removing everything after the first space with a standard variable expansion.

$ ( . /etc/os-release && printf '%s\n' "${VERSION%% *}" "$VERSION_ID" )
20.04.3
20.04

On your system, since the VERSION variable seems to contain the string 4.4 and nothing else, you could source the file and use "$VERSION" without further processing.

Kusalananda
  • 333,661
1

Using lsb_release to get the release number:

variable=$(lsb_release -rs)

man lsb_release:

-r, --release       displays release number of distribution.
-s, --short   displays all of the above information in short output format.
GAD3R
  • 66,769
-1

Thanks for the above answers as they were really helpful, but I found another solution from https://unix.stackexchange.com/a/498788 which states that we can store the value from os-release as

VERSION=$(grep -oP '(?<=^VERSION_ID=).+' /etc/os-release | tr -d '"')
echo $VERSION
G Man
  • 1
  • 1