How to disable "WARNING: apt does not have a stable CLI interface..."

Solution 1:

First, consider the meaning of the warning you're trying to hide. In theory, apt could change tomorrow to calling them "distributions" instead of "packages" (because it "does not have a stable CLI interface yet") and this would completely break your pipeline. A more likely change would be one which uses the word "packages" in multiple places, causing your pipeline to return extraneous information instead of only the package count you're looking for.

But you're probably not too worried about that, and, realistically, there's no reason you should be. The interface has been stable for years and probably isn't changing any time soon. So how do you make that warning go away?

In the *nix world, output to the command line is generally of two flavors, stdout (standard output) and stderr (standard error). Well-behaved programs send their normal output to stdout and any warnings or error messages to stderr. So, if you want errors/warnings to disappear, you can usually accomplish this by throwing away any messages on stderr using the output redirection 2>/dev/null. (In English, that's "redirect (>) the second output channel (2, which is stderr) to /dev/null (which just throws away everything sent there)".

The answer, then, is:

$ sudo apt update 2>/dev/null | grep packages | cut -d '.' -f 1
4 packages can be upgraded

Side note: In the question, your command is shown as # sudo apt.... The # shell prompt implies that you were probably logged in as root when using that command. If you're already root, you don't need to use sudo.

More on the warning you want to ignore (from man apt):

       The apt(8) commandline is designed as a end-user tool and it may change
       the output between versions. While it tries to not break backward
       compatibility there is no guarantee for it either. All features of
       apt(8) are available in apt-cache(8) and apt-get(8) via APT options.
       Please prefer using these commands in your scripts.

Solution 2:

You can use an alternative with the following command

sudo apt-get -s upgrade | grep -P "\d\K upgraded"

The output should be like this

6 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

The -s means simulate, recon, or dry run

from the apt-get man pages

       -s, --simulate, --just-print, --dry-run, --recon, --no-act
           No action; perform a simulation of events that would occur based on
           the current system state but do not actually change the system.
           Locking will be disabled (Debug::NoLocking) so the system state
           could change while apt-get is running. Simulations can also be
           executed by non-root users which might not have read access to all
           apt configuration distorting the simulation. A notice expressing
           this warning is also shown by default for non-root users
           (APT::Get::Show-User-Simulation-Note). Configuration Item:

This answer was inspired by this blog