How to execute multiple commands with sudo in script [duplicate] How to execute multiple commands with sudo in script [duplicate] shell shell

How to execute multiple commands with sudo in script [duplicate]


you can run sh -c ..., but remember to quote properly.

sudo sh -c 'id; echo another command ; id'

sudo must see this as a single argument for the sh command.

Of course you can use new line instead of semicolon:

sudo sh -c '  echo "I am root"  id  echo "another command"  id'


One way is to write a script with all the things you need to do with sudo and then run the script with sudo.


you could put all your commands in a script. Then

  • sudo ./script.sh
  • put permissions for script.sh in /etc/sudoers.d; that way you'll never need to type your password again (for that script)