How to run multiple commands in sudo under Linux or Unix

You can run multiple commands as sudo by separating the commands with a semicolon (;) or by using a shell script.

  1. Using semicolon:
sudo command1 ; sudo command2 ; sudo command3
  1. Using a shell script:

Create a shell script with the commands you want to run, make it executable and then run it with sudo:

#!/bin/bash

command1
command2
command3

chmod +x script.sh
sudo ./script.sh

Note: Running multiple commands as sudo can be dangerous, as it gives the commands elevated privileges. Make sure that the commands you run with sudo are safe and necessary.

Leave a Comment