How to Run Multiple Linux Commands at Once in Terminal

Running two or more commands in one line can save you a good deal of time and help you become more efficient and productive in Linux.
Warp Terminal

Running two or more commands in one line can save you a good deal of time and help you become more efficient and productive in Linux.

There are three ways you can run multiple commands in one line in Linux:

Operator Example Explanation
; Command 1 ; Command 2 Run command 1 first and then command 2
&& Command 1 && Command 2 Run command 2 only if command 1 ends sucessfully
|| Command 1 || Command 2 Run command 2 only if command 1 fails

Let me show you in detail how you can chain commands in Linux.

Using ; to run multiple Linux commands in one line

The simplest of them all is the semicolon (;). You just combine several commands that you want to run using ; in the following fashion:

cmd1; cmd2; cmd3

Here, cmd1 will run first. Irrespective of whether cmd1 runs successfully or with error, cmd2 will run after it. And when cmd2 command finishes, cmd3 will run.

Let’s take an example you can practice easily (if you want to).

mkdir new_dir; cd new_dir; pwd

In the above command, you create a new directory named new_dir with mkdir command. Then you switch to this newly created directory using cd command. Lastly, you print your current location with pwd command.

Running Multiple Commands Linux
Running Multiple Commands Linux with ;
πŸ“‹
The space after the semicolon (;) is optional but makes the chain of commands easily readable.

Using && to run multiple Linux commands

Sometimes you want to ensure that the in the chain of Linux commands, the next command only runs when the previous command ends successfully. This is where the logical AND operator && comes into the picture:

cmd1 && cmd2 && cmd3

If you use Ubuntu or Debian based distributions, you must have come across this command that utilizes && concept:

sudo apt update && sudo apt upgrade

Here, the first command (sudo apt update) refreshes the package database cache. If there is no error, it will then upgrade all the packages that have newer versions available.

Let’s take the earlier example. If the new_dir already exists, mkdir command will return an error. The difference in the behavior of ; and && can be seen in the screenshot below:

Multiple Linux Commands One Line

Did you see how commands separated by && stopped when the first command resulted in an error?

Using || to run several Linux commands at once

You can use the logical OR operator (||) to run a chain of commands but the next command only runs when the previous command ends in error. This is opposite to what you saw with &&.

cmd1 || cmd2 || cmd3

If cmd1 fails, cmd2 runs. If cmd2 runs successfully, cmd3 won’t run.

Linux Chain Commands

In the screenshot above, mkdir new_dir command fails because `new_dir already exists. Since this command fails, the next command cd new_dir is executed successfully. And now that this command has run successfully, the next command pwd won’t run.

Bonus Tip: Combine && and || operators

You may combine the operators to run two or more Linux commands.

If you combine three commands with && and ||, it will behave as the ternary operator in C/C++ ( condition ? expression_true ; expression_false).

cmd1 && cmd2 || cmd3

For example, you can check if file exists in bash, and print messages accordingly.

[ -f file.txt ] && echo "File exists" || echo "File doesn't exist"

Run the above command before and after creating the file.txt file to see the difference:

Combine Multiple Commands Linux

You can also use ;, && and || to run multiple commands in bash scripts as well.

More such Linux terminal tips

Like copy-paste in Linux terminal, running multiple commands at once is also one of the many Linux command line tips for saving time.

21 Super Handy Linux Command Tips and Tricks That Will Save you a lot of Time and Increase Your Productivity
Here are some tiny but useful Linux commands, terminal tricks and shortcuts that will save you a lot of time while working with Linux command line.

Though elementary, it is an essential concept any Linux terminal user should know. Here are a few more.

19 Basic But Essential Linux Terminal Tips You Must Know
Learn some small, basic but often ignored things about the terminal. With the small tips, you should be able to use the terminal with slightly more efficiency.

And if you are absolutely new to Linux commands, I have created a series of tutorials that will help you.

Linux Command Tutorials for Absolute Beginners
Never used Linux commands before? No worries. This tutorial series is for absolute beginners to the Linux terminal.

I hope you liked this terminal trick. Stay tuned for more Linux command tips and tools.

About the author
Abhishek Prakash

Abhishek Prakash

Created It's FOSS 11 years ago to share my Linux adventures. Have a Master's degree in Engineering and years of IT industry experience. Huge fan of Agatha Christie detective mysteries πŸ•΅οΈβ€β™‚οΈ

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

itsfoss happy penguin

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.