So I am trying to create a bash/executable, and it in I need to know the version number of Ubuntu on the system. All the ways I have found online consist of lsb_release -r
, however I cannot output this into a variable.
Is there any way to get the current version of Ubuntu and save as a variable in a shell executable?
Should do the trick.
For the numeric portion only add this:
The
lsb-release
variables file/usr/bin/lsb_release
is a Python script. It's a short script that serves as a good introduction to the python language. As others mentioned, a shorter way to get the version number only is withlsb_release -sr
.The
/etc/lsb-release
file defines environmental variables with the same information provided by thelsb_release -a
command:You can include these environment variables at anytime using
. /etc/lsb-release
. To test in your terminal:An alternative is to use the
/etc/os-release
file instead. This is formatted as a list of shell variables:So an easy way of parsing it is to simply
source
the file:To avoid setting all these variables unnecessarily, you can source the file in a subshell, echo the variable you need and exit the subshell:
Alternatively, you can always just parse the file directly:
The
lsb_release
command supports an-s
(or--short
) option to print just the information you ask for and not the header that says what kind of information that is.To get just the version number it is thus sufficient to run:
For example, on Ubuntu 18.04 LTS, that outputs:
As with the method in WinEunuuchs2Unix's answer, it is still reasonable to use command substitution to assign this output to a shell variable. Supposing you wanted the
ver
variable to hold the release number:With
-s
, there's no need to parse out the number withcut
,sed
,grep
,awk
, more complex forms of parameter expansion, or the like.In this usage, the
"
"
quotes are optional, but I generally suggest quoting parameter expansion and other shell expansions except when there is a reason not to.Short and simple
lsb_release
commands.To print version only
Output:
To print description
Output:
About flags used here:
Shortening WinEunuuchs2Unix answer a bit:
On Ubuntu 20.04, the following is shown: