1. Overview

The sudo command is a powerful utility that enables us to execute commands as another user, typically with root privileges or another superuser. This way, sudo can enhance the security and flexibility of a Linux or Unix system by limiting the access and privileges of different users.

In this tutorial, we’ll learn how to execute multiple commands using a single sudo operation with different options and techniques.

2. Using Semicolon

Semicolon (;) is a special character that we can use to separate multiple commands in a single line.

When we use a semicolon, the shell executes all commands one after another, in the order they appear. This means that the first command must finish before the second runs, and so on.

For example, we may want to run two commands with root privileges:

$ sudo sh -c 'command1; command2'

This sh command spawns a new shell. In addition, -c tells this new shell to read the commands within the string argument by starting from the first one. The semicolon separates all commands.

Moreover, we can add the option to get the same result with a bit more safety:

$ sudo -- sh -c 'command1 & command2'

Here,   indicates that sudo should stop processing command line arguments and treat the rest of the arguments as the command to run.

For example, if we want to update our system, and then reboot it, we can use semicolons to separate the commands:

$ sudo sh -c 'apt-get update; apt-get upgrade; reboot'

This command runs apt-get update first and then apt-get upgrade and then reboot after that.

Additionally, we can also specify a different user with the -u option:

sudo -u megalous -- sh -c 'command1; command2'

This command prompts for the password of user megalous and executes the command as that user but with sudo privileges.

Furthermore, using semicolons can be useful when we want to run multiple commands that depend on each other or have a specific order.

3. Using Ampersand

An ampersand (&) is another special character that we can use to separate multiple commands in a single line.

Although they execute sequentially, when we use ampersand, all commands run in parallel, or at the same time. This means that the shell doesn’t wait for the first command to finish before running the second command, and so on. Instead, each one runs in the background and returns the prompt immediately.

For example, we may want to run two commands with root privileges:

$ sudo sh -c 'command1 & command2'

Here, ampersand tells the shell that the commands are different from each other.

Let’s suppose we want to run two commands that take a long time to complete, such as downloading a large file and compressing a directory:

$ sudo sh -c 'wget https://websiteexample.com/file.zip & tar czvf folder.tar.gz folder'

This command runs in the background and frees up the terminal for other tasks. We can check the progress with the jobs command and use the fg command to bring them to the foreground if we like.

Additionally, we can also use the -u option to run commands as a different user while using ampersand:

$ sudo -u megalous -- sh -c 'command1 & command2'

This code runs under the user megalous regardless of the original execution context.

4. Using Logical Operators

Another way we can run multiple commands with sudo is to use logical operators, such as && (AND) or || (OR). We can use the operators to create conditional statements that control when and how our commands execute.

4.1. AND Operator (&&)

We use the AND operator to run commands only if both of the conditions we specify are true.

Let’s suppose we want to run two commands with root privileges. For example, we create a directory named backup and copy all files from our home directory to it:

$ sudo sh -c 'mkdir backup && cp -r $HOME/* backup'

This prompts for the user’s password and runs the commands mkdir backup and cp -r $HOME/* backup with root privileges, using sh as the shell.

Moreover, the && operator ensures that the second command is executed only if the first command succeeds. If the first command fails, for example, if the directory backup already exists, the second command won’t be executed.

4.2. OR Operator (||)

Additionally, we should use the OR operator to run commands if we want them to run when at least one of the conditions is true.

Let’s suppose we want to run two commands with root privileges, such as installing a package named foo and updating the system:

$ sudo sh -c 'apt-get install foo || apt-get update'

The code above runs the commands apt-get install foo and apt-get update with root privileges, using sh as the shell.

Moreover, the || operator ensures that the second command executes only if the first command fails. If the first command succeeds, for example, if the package foo is already installed, the second command won’t be executed.

4.3. Combining Both Operators

Furthermore, we can combine both logical operators to create more conditions to run under one single sudo.

Let’s suppose we want to run three commands with root privileges, such as creating a file named test.txt, writing the word hello to it, and deleting it:

$ sudo sh -c 'touch test.txt && echo hello > test.txt || rm test.txt'

The code runs the commands touch test.txt, echo hello > test.txt, and rm test.txt with root privileges, using sh as the shell.

The && and || operators create a conditional expression that executes the commands:

  • if the first command succeeds, i.e., if the file test.txt is created successfully, the second command executes
  • if the first command fails, i.e., if the file test.txt already exists or cannot be created, the third command executes
  • if the second command fails, i.e., the word hello cannot be written to the file, the third command executes

Moreover, it’s advisable to be careful with logical operators and understand clearly how they work before employing them.

5. Conclusion

In this article, we explored how we can run multiple commands with a single sudo operation.

First, we looked at how we can use the semicolon and ampersand symbol to separate arguments. Additionally, we learned how we can use logical operators with the sudo commands with examples.

However, it’s important to take care with the sudo command because it operates with superuser privileges.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.