8

I try to write a script to get the version of my distro so that I can pass it to a variable.
The following command is what I wrote to achieve the result.

lsb_release -ar | grep -i release | cut -s -f2

The unwanted output:

No LSB modules are available.
18.04

As you can see, the No LSB modules are available message is the unwanted part.
Since I prefer my script to be portable across servers, I don't want to install any extra packages beside utilizing the lsb_release -a command.

doneal24
  • 5,059

3 Answers3

10

Stumbled upon this via search results. Doesn't the following work across your systems? Updated for Ubuntu 23.04 given @stephen-kitt's suggestion.

$ lsb_release -sr 2>/dev/null
23.04

That gives you the short release output.

As you write that you want a variable.

export RELEASE=$(lsb_release -sr 2>/dev/null)

Should do exactly what you would need, or?

JoKi
  • 101
  • Welcome to the site, and thank you for your contribution. The key part of the OPs question seems to be that on some platforms there is an unwanted additional message, namely No LSB modules are available. Can you confirm that your solution - in addition to printing only the bare version number - also suppresses that message? – AdminBee Apr 24 '20 at 09:41
  • 4
    Confirmed, it does suppress the message and displays only the version number.

    The reason that the OP gets the unwanted information is because the package lsb-core is not installed.

    – JoKi Apr 26 '20 at 05:59
  • lsb_release -a does the trick too – NanoNova Aug 25 '22 at 16:06
  • Sadly, this seems to have stopped working on Ubuntu 23.04? Might well be a bug with lsb_release, but lsb_release -sr DOES show me "No LSB modules are available." as well :/ – Michael Große Jun 08 '23 at 15:30
  • Hi there, yes you're right. I just checked it on 23.04. However using @stephen-kitt's suggestion to redirect the standard error works nicely.

    $ lsb_release -sr 2>/dev/null

    – JoKi Jun 09 '23 at 17:06
7

That message is sent to standard error, so redirecting that to /dev/null will get rid of it (along with any other error message produced by lsb_release):

lsb_release -ar 2>/dev/null | grep -i release | cut -s -f2
Stephen Kitt
  • 434,908
0

I try to write a script to get the version of my distro

If you want a simple solution :

$ lsb_release -sr
22.04

If you don't have lsb_release on your Linux distribution :

$ (. /etc/*-release; printf '%s\n' "$DISTRIB_RELEASE";)
22.04

Reference