Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a shell script in current shell with sudo permission

For executing a shell script in current shell, we need to use a period . or a source command. But why does it not work with a sudo permission?

I have a script with execute permission called setup.sh. When I use a period, I get this:

$ sudo . ./setup.sh  sudo: .: command not found 

The source command also produces a similar error. Am I missing out something? What should I do to run the script with sudo permission in the same/current shell?

Thanks in advance..

like image 963
shar Avatar asked Sep 15 '13 06:09

shar


People also ask

How do I run a .sh file in sudo?

Therefore, to run a shell script or program as root, you need to use sudo command. However, sudo only recognizes and runs commands that exist in directories specified in the secure_path in the /etc/sudoers, unless a command is present in the secure_path, you'll counter an error such as the one below.

How do I give permission to execute a shell script in Linux?

We can provide the executable permission by using the below command, chmod +x filename.sh. chmod (Change Mode) - Using chmod we can change the access permissions to file system objects. +x - It makes the file executable.

How do I run a .sh file in Terminal as root?

SH file as a root User. Type your user password and hit Enter to execute the . SH file. You can also use su command to login with superuser and execute the .


1 Answers

I'm not sure if this breaks any rules but

sudo bash script.sh 

seems to work for me.

like image 136
JaseC Avatar answered Oct 05 '22 20:10

JaseC