Shell – Should I be using ‘sudo’ in scripts that I write

scriptingshellsudoUbuntu

I'm setting up a new machine (well actually, an Ubuntu VM) and am trying to write a script to setup a few common things that I use when doing this (Git, curl, vim + janus).

So my script looks a bit like this:

#setup
#!/bin/sh
sudo apt-get install git
sudo apt-get install curl
...

It doesn't seem great to have 'sudo' mixed in with my command — that is just my security-spidey-sense tingling. It seems like something like the following might also work:

sudo setup

Is there a better way to do this? What are your rules of thumb when you write scripts and need elevated permissions?

Best Answer

There is no problem using multiple 'sudo' calls in scripts.

I find it better than running the whole scripts as root as the risks are limited by restricting the privilege elevation to the commands that really need them.

Related Question