36

I'm trying to write a script which will output the number of upgrade-able packages from apt. However it keeps giving me this warning with it also:

# sudo apt update | grep packages | cut -d '.' -f 1

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

All packages are up to date

I would like it to just output either:

All packages are up to date

or

35 packages can be updated

Is there any way to disable that warning? I will be using this returned string, along with some extra information, in a Discord notification from a cron job and it messes up my output pretty wickedly.

I already looked at these, but none of them worked for me:

https://askubuntu.com/questions/49958/how-to-find-the-number-of-packages-needing-update-from-the-command-line

https://unix.stackexchange.com/questions/19470/list-available-updates-but-do-not-install-them

https://askubuntu.com/questions/269606/apt-get-count-the-number-of-updates-available

1
  • 4
    if you want to suppress the warning use apt-get instead of just apt. The problem is that apt-get doesn't provide the number of updates. You can eventually use in your script: sudo apt-get update 2> /dev/null to show only errors if there are some and then execute sudo apt-get upgrade --dry-run | grep upgraded. The output of the second command will show you only: 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. and --dry-run won't run the upgrade. Feb 14, 2020 at 15:27

2 Answers 2

30

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):

SCRIPT USAGE
       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.
3
  • Ah, thank you so much! This is exactly what I was hoping for. I realize that this may change in the future, but in my case if the output changes its not a big deal as I can just change the script. Mar 13, 2019 at 21:46
  • 5
    According to the source code this is controlled by Apt::Cmd::Disable-Script-Warning github.com/Debian/apt/blob/main/apt-private/private-main.cc
    – so_
    Nov 4, 2022 at 20:25
  • 1
    Which could be done with 'Apt::Cmd::Disable-Script-Warning "true";' in a file like /etc/apt/apt.conf.d/99disable-script-warning
    – so_
    Jan 24, 2023 at 20:13
7

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:
           APT::Get::Simulate.

This answer was inspired by this blog

2
  • The problem with this, and I have tried this, is the output is not what I would want for my application. Mar 13, 2019 at 21:44
  • 1
    Using apt-get instead of apt fixes the warning, that's what I came here for!
    – dreua
    Apr 6, 2021 at 5:41

Not the answer you're looking for? Browse other questions tagged .