14

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?

2
  • Yes, I do appreciate it. Sorry, I wasn't aware there was documentation on this. I appreciate it anyhow Jul 31, 2019 at 19:53
  • Yes, I have done it. Thanks Jul 31, 2019 at 20:04

5 Answers 5

27
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
12
  • so yes thank you, it does work, it outputs Release: 18.04. However is there a way to just get the number? I want to be able to use this variable in an if/else function in the script. The purpose of the if/else is to download the correct version of my application for the distro of ubuntu. Jul 31, 2019 at 19:51
  • Ah nevermind. @user68186 solved my issue. Thank you guys so much! Jul 31, 2019 at 19:52
  • I was going to write the answer but you beat me to it! :-) I am happy to contribute.
    – user68186
    Jul 31, 2019 at 19:53
  • 4
    @a.mosallaei a word of caution - if you're just using the number part and ignoring everything else, this script is going to have weird results on any non-Ubuntu machine (including many Ubuntu-based distributions that are Ubuntu "compatible" but have their own names and versioning); you might need to verify if the lsb_release result starts with "Ubuntu" before using the number.
    – Peteris
    Aug 1, 2019 at 22:53
  • 1
    @a.mosallaei You say that your executable will only be used on Ubuntu computers. How do you know that? It is better to have a failsafe that exits if the distro is not Ubuntu. Trust me, users will try to crash your program. Aug 2, 2019 at 7:21
12

An alternative is to use the /etc/os-release file instead. This is formatted as a list of shell variables:

$ cat /etc/os-release 
NAME="Ubuntu"
VERSION="18.04.2 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.2 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

So an easy way of parsing it is to simply source the file:

$ . /etc/os-release

$ echo $NAME
Ubuntu

$ echo $VERSION
18.04.2 LTS (Bionic Beaver)

$ echo $PRETTY_NAME
Ubuntu 18.04.2 LTS


$ echo $VERSION_ID
18.04

To avoid setting all these variables unnecessarily, you can source the file in a subshell, echo the variable you need and exit the subshell:

$ ( . /etc/os-release ; echo $VERSION_ID)
18.04

Alternatively, you can always just parse the file directly:

$ grep -oP 'VERSION_ID="\K[\d.]+' /etc/os-release 
18.04
3
  • 2
    Note that for Ubuntu derivatives, the version info might refer to the downstream version (e.g. I've got VERSION_ID="18.3" and VERSION_CODENAME="sylvia" for my Mint box). UBUNTU_CODENAME should be correct, though. Aug 1, 2019 at 10:58
  • subshell is the most robust way to do this, right? Aug 1, 2019 at 14:40
  • @JamesTheAwesomeDude probably, yes.
    – terdon
    Aug 1, 2019 at 14:46
11

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:

lsb_release -sr

For example, on Ubuntu 18.04 LTS, that outputs:

18.04

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:

ver="$(lsb_release -sr)"

With -s, there's no need to parse out the number with cut, 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.

1
  • Yes, thank you! I never understood the -s, but you explained it perfectly! Appreciated Aug 1, 2019 at 13:36
7

Short and simple lsb_release commands.

  • To print version only

    lsb_release -sr
    

    Output:

    18.04
    
  • To print description

    lsb_release -sd
    

    Output:

    Ubuntu 18.04.2 LTS
    

About flags used here:

-s   show requested information in short format
-r   show release number of this distribution
-d   show description of this distribution
1

Shortening WinEunuuchs2Unix answer a bit:

distro=$(lsb_release -i |cut -f2)
echo "${distro,,}"

os_version=$(lsb_release -r |cut -f2)
echo "${os_version}"

On Ubuntu 20.04, the following is shown:

ubuntu
20.04
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .