I'm about to write a script that requires some commands be executed with sudo and others be executed as the current user. I believe that if I sudo the script with sudo script, then all the command in it will run as sudo. How to I exclude some of them to run as the current user?

1 Answer 1


Prefix each command that needs sudo in the script with sudo, in the script, and run the script as normal. (This can be a bit surprising when the first password prompt comes up, so it’s worth adding a message before the first sudo in the script).

  • This is actually going to run on Ubuntu within AWS, which never asks for the password, I don't know why. tyvm.
    – Chuck
    Commented Mar 17, 2017 at 17:36
  • @Chuck then you are probably logging in as root. Don't do that! In any case, if you are logging in as root, using sudo or not will make no difference.
    – terdon
    Commented Mar 17, 2017 at 17:56
  • I appreciate that tip, but I'm certain I'm not logging in as root. Some commands require sudo when I'm logged in and won't work without it.
    – Chuck
    Commented Mar 17, 2017 at 18:07
  • 2
    Your sudo is probably set up to not require a password then; look for NOPASSWD in /etc/sudoers. Commented Mar 17, 2017 at 18:32
  • 1
    This doesn't work well if the one or more of the commands takes a long time. You start the process, walk away, only to find it's paused waiting for you to type in your password again, repeat for each long command.
    – gman
    Commented Feb 28, 2023 at 1:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .