This may be a simple question but...

I have an update.sh file with the following:

sudo apt-get update
sudo apt-get upgrade

When I run this using ./update.sh (after setting it to executable), it asks me to enter my sudo password. Will this run both commands, and I can essentially only run this script once and have my apt-get update/upgrade commands taken care of?

Thanks everyone

  • Your script should ask for your password only once while runing both commands. Note that as written, the script is interactive - it will ask you questions and wait for your response, even if you're not there. Er, Ubuntu does ship with unattended-upgrades, which does all this for you, if you wish. – user535733 Apr 29 '17 at 17:38
  • could you give some more details on what you mean? I'm just trying to only have to run one command for both update/upgrade. I'm not too familiar with bash scripts and the like. – snovosel Apr 29 '17 at 18:05
  • apt already has a feature to update/upgrade daily. It's called unattended-upgrades, and it's included with all flavors with Ubuntu. You need to understand a bit about how packages and repositories work (which is good - you need to know that anyway). The settings for unattended-upgrades are within apt: /etc/apt/apt.conf.d/ – user535733 Apr 29 '17 at 19:54
  • is there documentation for this? – snovosel Apr 29 '17 at 22:18
  • Yes, each setting is explained within the files themselves. Be aware that apt will do exactly what you tell it to do, regardless of your actual intent. For example, updating from non-Ubuntu repos and PPAs can be quite unwise. – user535733 Apr 29 '17 at 22:43

The way sudo works is that by default it gives you 15 minutes before it will ask for password again. So yes, using the two commands, will work perfectly fine, and depending on how long apt-get update runs you will need to enter password once.

However, this is not the best practice. Instead, I would suggest making your whole script run with sudo instead of placing it in front of each command in file. In other words, the script should be:

apt-get update
apt-get upgrade

And run it as

sudo  ./my_script.sh

Notice that here we're using #!/bin/bash line to use bash shell as interpreter. This is very different from /bin/sh. For what you're doing it makes no difference, but if you need to be aware that they differ in syntax of some of the operators.

However, even better approach would be to get rid of script altogether and use function or alias that does what you want. Scripts are really for large amounts of code that you want to reuse. For 2 - 3 lines a function or alias in ~/.bashrc file that's going to be sufficient.

Personally I'd make something like this in my ~/.bashrc

alias agu="sudo bash -c 'apt-get update && apt-get upgrade'"
| improve this answer | |

Don't forget to set DEBIAN_FRONTEND=noninteractive for those pesky prompts about which version to maintain:

sudo DEBIAN_FRONTEND=noninteractive apt-get upgrade -y;

If you don't do this your script will hang in patching limbo. So if you were going to do this in a script I would just do the following:

#!/usr/bin/env bash
set -e

    sudo apt-get update -y;
    sudo DEBIAN_FRONTEND=noninteractive apt-get upgrade -y;


The previously stated methods would work most of the time (if they had the -y flag), until they wouldn't work due to an interactive prompt.

Hopefully, it helps someone else with their patching/bootstrap script.

| improve this answer | |

Why not simply run:

apt-get update && apt-get upgrade
| 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.