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
More on the warning you want to ignore (from
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.