Run multiple Linux commands at once
How To

Run Multiple Commands in One Line in Ubuntu

Pranav Krishna
Pranav Krishna

Table of Contents

You have probably seen multiple commands in a single line, executing at once, and you might have tried some without realizing or understanding what exactly is going on.

Running multiple commands at once is a time-saving skill while dealing with the command line, and every Linux user should know about them.

Let me help you understand what they are.

Control Operators in Bash

Basically, there are three operators that control the flow of the commands. They are helpful in combining different commands. They are:

  • 'semicolon' (or separator) ;
  • 'and' operator &&
  • 'or' operator ||

They modify the control flow based on the success or failure of commands, which we will discuss, one by one.

The semicolon (;) operator

The ; operator is the simplest of all. The commands separated by a semicolon are executed sequentially.

The syntax of this command is:

command-1; command-2; command-3
Syntax of ;

Here, command-2 is executed whether or not command-1 runs successfully, irrespective of the output or exit status. Upon the end of command-2, the command-3 will run too.

This can also be seen as a replacement for entering every command on a new line, in case you are working with a shell script.

Let us see an example.

mkdir new_Dir; cd new_Dir; touch newfile; ls
Usage of semicolon separator

Here, a new directory (new_Dir) is created, then I switched to it, created a new file, and listed the contents of the directory. Even though one of the commands fails, the control flow is not broken; all commands are executed.

The and (&&) operator

The and (&&) operator is used to combine two commands, which behaves differently compared to the semicolon.

It executes the next command in the line if the output of the previous command turns out to be successful. That is just "exit code 0" in simpler terms.

The general syntax is shown below.

command-1 && command-2
Syntax of &&

Where command-2 executes if and only if command-1 executes successfully. Else, command-2 is not executed. The preceding command must return an exit status 0 else the next command is not executed.

You might have seen, or even used this example more than you think, in Ubuntu:

sudo apt update && sudo apt upgrade 
The and (&&) operator execution example
The and (&&) operator

If sudo apt update fails, the upgrade command does not execute. Any exit code other than '0' will not execute the other command.

Exit codes are another interesting topic too. There's a whole universe beyond the exit codes 0 and 1, which you can read about here:

Exit Codes in Linux [Explained]
Unraveling the mystery of exit codes in Linux. Learn what are the exit codes and why and how they are used.

The or (||) operator

We can understand the 'or' operator like this, for the given syntax:

command-1 || command-2
Syntax of 'or' operator

"Execute either the command-1 or the command-2, but not both." It works opposite to the 'and' operator, by executing command-2 only if command-1 fails.

Let's use the first example to demonstrate this operator.

mkdir newDir || cd newDir
The 'or' operator example
The 'or' (||) operator

If mkdir fails, which implies the directory already exists, then the path is switched to that directory. If it does not exist already, then the directory is created (the first command is executed).

Bonus: Combining these operators

We can combine such operators in all sorts of ways to perform different operations based on select requirements.

A probably uncommon example would be checking the existence of something, like this:

[ -e apt ] && echo "Debian based distro" || echo "Not debian based"
Output of the conditional statement given above in Ubuntu and Fedora
The output of the above command (in Ubuntu and Fedora)

See? That's how exciting these operators can be! Single-line conditional statements, response-based execution of commands, you name it. A lot can be achieved by combining operators accordingly.

Recommended Read 📖: Wondering how I showed about the distribution I'm using? Check out this article that shows multiple ways regarding this.

Check Ubuntu Version in Command Line
Learn how to know the Ubuntu version number and release codename in the command line.

Learn more commands to extend the chain!

Now, you know how to chain commands, it's time to learn new commands and I would recommend you to start with the basics.

And for that purpose, we made a dedicated tutorial:

25 Must-Know Ubuntu Commands
New to Ubuntu? Here are the must know Ubuntu commands that will help you get control on the system more effectively in the terminal.

I hope you will find this guide helpful.

Pranav Krishna

Pranav Krishna

This is Pranav Krishna, a Proud Linux user in twelfth grade.