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:
And run it as
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
alias agu="sudo bash -c 'apt-get update && apt-get upgrade'"