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?


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 Mar 17 '17 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 Mar 17 '17 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 Mar 17 '17 at 18:07
  • 2
    Your sudo is probably set up to not require a password then; look for NOPASSWD in /etc/sudoers. – Stephen Kitt Mar 17 '17 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.