Suppress Irritating Warning Messages From `apt`

Stickman says what

Yes, really.

You know the error messages you get any time you’re trying to pipe the output of apt to something? They’re kind of irritating since they don’t help in any meaningful way. e.g. you might want to reduce apt search results to packages that contain a string either in the name or description, but the error message is sure to appear:

Bash
apt search ufw | grep -A 1 ufw

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

gufw/noble 24.04.0-2 all
  graphical user interface for ufw

--
ufw/noble,now 0.36.2-6 all [installed,automatic]
  program for managing a Netfilter firewall

Or if you’re trying to get a list of installed packages in a way to which they can be referred to by scripts or other piping, there it is again…

Bash
apt list --installed | cut -d '/' -f1

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

Listing...
accountsservice
acl
adcli
. . . 

A slight caveat: If you’re actually scripting, there’s better commands to use than apt, such as dpkg or even apt-get (probably only still around because of it). Please research them if you’re working on something substantive or important, such as an installation script or Dockerfile you anticipate distributing.

But, if you’re just messing around in your shell doing the odd one-off, it’s nice to be able to use the command with which you’re probably most familiar, rather than having to look up more commands, flags, and modifiers.

The good thing is, there’s an easy fix: Create an alias that accepts any number of arguments (as a string), and pipe the stderr output to /dev/null:

Bash
alias apt="apt $* 2> /dev/null"

This will omit other error messages from apt while you’re using it, but in all my experience I can’t remember ever seeing one for a different reason.

Note: you need this alias to be encapsulated with quotation marks, since it contains spaces, but invoking an environment variable (the $* argument) cannot be done with single-quotes ', so be sure to use double-quotes "

Luckily, that leaves the single-quotes available to wrap the whole expression and append it to your rc (.bash_aliases, etc.):

Bash
echo 'alias apt="apt $* 2> /dev/null"' >> $HOME/.bashrc

If you’re interested in further reading, I recommend this short explanation of the differences between $*, "$*", $@ and "$@":
https://unix.stackexchange.com/questions/129072/whats-the-difference-between-and

Questions? 😊


Leave a Reply

Your email address will not be published. Required fields are marked *