Views:
16,793β
Votes: 27β
β
Solution
Tags:
command-line
bash
scripts
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/1162492
Title:
How can you tell the version of Ubuntu on a system in a .sh (bash) script?
ID:
/2019/07/31/How-can-you-tell-the-version-of-Ubuntu-on-a-system-in-a-.sh-_bash_-script_
Created:
July 31, 2019
Edited: August 1, 2019
Upload:
September 15, 2024
Layout: post
TOC:
false
Navigation: false
Copy to clipboard: false
Var=$(lsb_release -r)
echo "$Var"
Should do the trick.
For the numeric portion only add this:
NumOnly=$(cut -f2 <<< "$Var")
echo "$NumOnly"
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 with lsb_release -sr
.
The /etc/lsb-release
file defines environmental variables with the same information provided by the lsb_release -a
command:
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.6 LTS"
You can include these environment variables at anytime using . /etc/lsb-release
. To test in your terminal:
$ . /etc/lsb-release
$ echo $DISTRIB_RELEASE
16.04
$ echo $DISTRIB_DESCRIPTION
Ubuntu 16.04.6 LTS