Bash – Run entire bash script as root or use sudo on the commands that need it

bashshell-scriptsudo

I'm working on some installation script in bash (on a Raspberry Pi running Stretch). It will copy files to /usr/local/bin and to my user profile and it will install a few packages if needed. The script is almost 2000 lines and 20–30 commands need root.

Now my question is: should I run the entire script with sudo or just as standard user and sudo only the commands inside the script that need admin rights?

Best Answer

If you know for sure that running the script with sudo won't do you any harm (For example, it won't create new files that will now need root privileges, but wouldn't otherwise), you should run it with sudo.

If you know there are some side effects or you are unsure, do it the safe way and use sudo just where you must.

Related Question