run multiple commands in linux

How to Run Multiple Commands in Linux

While using Linux, you may need to run many commands one after the other. Many times, you may need to run multiple commands in one line or at once. In this article, we will learn how to run multiple commands in Linux. There are several ways to do this and we will understand each way in detail.


How to Run Multiple Commands in Linux

There are three ways to run multiple commands in Linux – using semicolon (;), AND (&&) and OR (||) operator.


1. Concatenate Commands using semicolon (;) operator

The semicolon operator allows you to execute multiple commands one after the other, irrespective of whether the previous one succeeded or failed. Here is the syntax for semicolon operator.

$ command 1; command 2; … command N

Here is an example to run whoami, ps and uptime commands one after the other.

$ whoami; pwd; uptime
ubuntu
/home/ubuntu
 03:35:48  up   2:00,  2 users,  load average: 0.49, 0.45, 0.51

As you can see, it will display the output of each command one after the other. Here is an example where the second command gives an error. You will find that the terminal still runs all the commands, and displays each command’s output.

pwd; cd data; uptime

/home/ubuntu
bash: cd: data: No such file or directory
 03:36:00  up   2:00,  2 users,  load average: 0.72, 0.50, 0.53


2. Concatenate Commands Using AND Operator

In this case, the terminal will run each command one by one, as long as its previous command runs successfully. If any of the command gives an error, then subsequent commands will not be executed. Here is the syntax of using AND operator.

$ command 1 && command 2 && … command N

Here is a simple command to create a new folder and switch to it.

$ mkdir data && cd data

Now if you try re-running the above command, the first command in it will give an error saying the folder already exists, and therefore will not execute the second command.

mkdir data && cd data

mkdir: cannot create directory ‘data’: File exists


3. Concatenate Commands with OR Operator

In this case, Linux will run each command one after the other, only if the previous command has failed. It stops executing after the first successful command execution. Here is the syntax to concatenate commands using OR operator.

$ command 1 || command 2 || … command N

Here is a simple example to concatenate commands using OR operator.

$ mkdir1 data1 || mkdir data2

When you run the above command, the first command will give an error since mkdir1 is a typo and then the second command will be executed. So only folder data2 will be created and not data1.

In this article, we have learnt 3 different ways to execute multiple commands at once in Linux. They are very useful, especially in shell scripts & cronjobs where you need to run multiple commands at once. They are also very helpful for system administrators who can run a batch of commands to automate tasks.

Also read:

How to Record & Replay Terminal Session in Linux
How to Save All Terminal Output to File
How to Remove Startup Applications in Ubuntu
How to Do Port Forwarding in Raspberry Pi
How to Install Raspbian in Raspberry Pi

Leave a Reply

Your email address will not be published. Required fields are marked *