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".

3
  • dpkg - l is a shortcut for dpkg-query - so just use that directly: dpkg-query --show --showformat '${Version}' systemd
    – muru
    Commented May 13 at 10:02
  • The dpkg-query --show --showformat '${Version}' systemd works, but I want to confirm if this command also works on the packages with "deinstall" status. Do you know how to make a package become the "deinstall" status for testing? See: askubuntu.com/questions/165951/…
    – stackbiz
    Commented May 13 at 11:30
  • If it shows up in the output of dpkg -l, it will show up in the output of dpkg-query. As I said, the former is just an alias for the latter
    – muru
    Commented May 13 at 11:47

1 Answer 1

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}'
0

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