1

I'm trying to detect if a package is installed in a bash script using the following but the script keeps erroring out and preventing anything after it from running.

Is there an option for apt that tells it to NOT throw an error when a package is not in the list?

pkgExists=$(apt list "azure-cli" | grep "azure-cli" -s)
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

1 Answers1

3

If a package is not in the list, apt list just shows Listing... Done and exits. If you try to pipe its output like you do however it throws a clear warning:

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Use dpkg-query --list instead, e.g.:

dpkg-query --list "azure-cli" && echo "exists" || echo "doesn't exist"

Note that dpkg-query --list doesn’t show packages that are not installed.

dessert
  • 1,687