1

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
Flimm
  • 4,218
B K Karthik
  • 41
  • 1
  • 5
  • 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. – Francesco Jun 15 '20 at 07:46
  • You will need to prefix the script name with ./ , like this: sudo ./script.sh – Flimm May 15 '23 at 13:25

2 Answers2

1

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.

pLumo
  • 22,565
1

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)