I am relatively new to Linux in general, still learning :)

I want to write a bash script for automation of apt update and apt upgrade. I have the idea of sending the apt update command, then watching the lines for specific words.

E.g, after apt update was done, it says:

 Reading state information... Done
 89 packages can be upgraded.

So in this case I want to look for can be upgraded. If those words are in the output the script shall execute apt upgrade -y.

Any help is highly appreciated. Thanks in advance.


There is this tool /usr/lib/update-notifier/apt-check giving simple output.

The --human-readable flag gives more information.

In short, /usr/lib/update-notifier/apt-check --human-readable gives two numbers :

<n1> packages can be updated.
<n2> updates are security updates.

/usr/lib/update-notifier/apt-check with no option has this output layout:


So it is possible to extract the first number and if it is greater than 0, say that some update is available.

Extract the first number, number of available updates:

/usr/lib/update-notifier/apt-check |& cut -d";" -f 1

Use it in a script:

if [ $(/usr/lib/update-notifier/apt-check |& cut -d";" -f 1) -gt 0 ]; then
    # Some commands
    sudo apt-get -y dist-upgrade
|improve this answer|||||
  • This is great! Thank you very much for the quick help :) – copperfrog Aug 19 '19 at 7:38
  • This is working for me really well. But I have another question: I found out that sometimes, when I use "apt list --upgradeable" it tells me, that there are some packages that can be updated, but when I run "/usr/lib/update-notifier/apt-check |& cut -d";" -f 1", the output is still 0. – copperfrog Sep 6 '19 at 9:35

In practice you don't care... Just run

apt update
apt upgrade

in sequence, if there is nothing to upgrade, nothing will happen. This is a script, and this is the computer doing the work, running apt upgrade isn't going to take much CPU.

The only tests worth doing here is that apt-update and apt-upgrade ran properly (no network errors, no half-installed/misconfigured packages).

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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