14

I have a simple shell script that is run with sudo as most of the script requires it, yet one of the commands in the script is a Homebrew install, which cannot be executed with sudo..

So, my question is when executing a shell script with sudo how do I execute sub commands as the current user and then continue the remainder of the script with sudo.

Prompting the user to enter his password again is not really practical as the script takes really long to execute and would require waiting 5-10 min for the prompt.

2 Answers 2

13

The easiest way is to run the subcommand via sudo from within the script. The user id to run with can be obtained by $SUDO_USER (look at the output of sudo env):

sudo -u $SUDO_USER ./exec_as_normal_user.sh
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to do this without the need for another bash script file? AWS allows for only one file as attachement when launching a server
0

Instantiate the shell using

sudo -u $USER_NAME bash

and execute the shell script by calling,

./program.sh

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.