If I write a shell script that has commands that require superuser privileges to execute, will that script run if I used sudo while executing the script? As in:

sudo script.sh
  • Did you try something? You could easily find the answer to your question just trying to write a script with a command that requires root privileges and running it with sudo. Jun 15 '20 at 7:46

Yes, all the commands in your script will run as root. See also.

But keep in mind that running your whole script with sudo will also affect some environment variables like $USER, which will be root instead of your normal users name, but depending on the sudo configuration it will not affect others, e.g. $HOME. See also.


Yes it will. Before that check the permissions if the script has the x (executable) permissions as well. Without it it won't run at all.

You can check that by listing the files in that directory with ls -la If there is no x in the permissions try to add it but be careful to not make it executable for all (e.x. 777)

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.