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
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
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)
./
, like this:sudo ./script.sh
– Flimm May 15 '23 at 13:25