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)

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.


Not the answer you're looking for? Browse other questions tagged or ask your own question.