2

I'm trying to get the installed version of systemd in the following command, but it returns all lines contain the keyword "systemd".

# dpkg -l | grep " systemd "

ii gnome-logs 42.0-1 amd64 viewer for the systemd journal ii libsystemd0:amd64 249.11-0ubuntu3.12 amd64 systemd utility library ii systemd 249.11-0ubuntu3.12 amd64 system and service manager ii systemd-container 249.11-0ubuntu3.12 amd64 systemd container/nspawn tools

How to make it return only the following line:

# dpkg -l | grep "__the_rule_for_systemd_"

ii systemd 249.11-0ubuntu3.12 amd64 system and service manager

And then, I can use awk to get the version:

# dpkg -l | grep "__the_rule_for_systemd_" | awk '{print $3}'

Here is the expected output:

249.11-0ubuntu3.12

How to write the "__the_rule_for_systemd_" for grep or is there any other command can get the installed version of systemd?

Note:

# package_name="systemd"
# dpkg -l | grep " $package_name "

The "__the_rule_for_systemd_" should also work on other packages, it should be able to get any package by that rule, not just only for "systemd".

muru
  • 207,228
stackbiz
  • 495

1 Answers1

1

This should do it:

apt-cache policy systemd | grep Installed

If you only want to return the version number (for a script), search and cut the string with awk:

apt-cache policy systemd | awk '/Installed/ {print $2}'
Artur Meinild
  • 31,035