The Linux terminal is a powerful command-line tool that helps allows users to run multiple commands to do a specific task.
So, as a Linux system administrator, the user may need to know about various commands and how to execute these commands at once on the terminal.
Running multiple commands at once or in one line is a more productive and efficient way in Linux. Moreover, it also saves a good deal of time.
In this tutorial we will learn how to run multiple Linux commands in one line using AND (&&)
, OR (||)
, and Semicolon (;)
operators.
Table of Contents
Running Multiple Commands in One Line
There are three different ways available for running multiple commands in one line in Linux that are listed:
- Using the
;
(semicolon) operator - Using
&&
(AND) operator - Using the
||
(OR) operator
Using the ; (semicolon) operator
The most common practice is using a semicolon (;
) between the chain of Linux commands. Using the semicolon (;
) operator, we can run multiple in a sequence irrespective of whether the previous command was executed successfully or failed. The basic syntax of using the semicolon (;) operator is:
command1 ; command2 ; command3
For instance, we have three different commands: command1, command2, and command3 which are separated by a semicolon (;
) operator.
This operator between three different commands ensures that all commands are executed sequentially one after another regardless of the output of the previous command.
So, we can use the (;
) operator, if you have to run multiple unrelated terminal commands and the output of the first command doesn’t affect the output of the next command.
Let’s take an example. We have three commands whoami
(displays the login name), date
(displays the current date), and hostname
(displays the system hostname). These commands are separated by semicolons (;
). You will notice that the terminal executes all these commands in the sequential order you have mentioned.
whoami ; date ; hostname
The above command will first execute the whoami command, and then timezone information and at the end, it will display the hostname.
Using && (AND) operator
If we want to run the chain of commands one by one in Linux that is related to the previous command, we can use the AND (&&)
operator in this case. The AND operator only allows the user to run the next command when the previous command is executed successfully. So, we can use the logical AND (&&)
operator between the commands in the following way:
command1 && command2 && command3
Example
For example, we want to view the items in the Downloads directory. For this purpose, we need to navigate into the downloads directory and then can list files.
cd Downloads && ls
Let’s take another example, we have created a new directory in the current directory and then using the cd command navigate inside the directory.
mkdir personal && cd personal
The first command will give an error of an already existing directory. Therefore, the next cd command will not run on your terminal.
Using the || (OR) operator
The OR (||)
the operator allows users to run the next command if the previous command fails and gives an error. But, if the previous command is executed successfully then, the next command will not run. Therefore, the OR (||)
operator is used to concatenate multiple commands.
Example
For example, we want to create a directory that already exists with the same name in the current working directory. In this case, command1 will give an error and the next command will execute on the system.
mkdir personal || cd personal || pwd
So, we can say that the second command command2
will only execute if the execution of command1
fails.
Combining && (AND) and || (OR) operators
We can also combine Linux commands by combining more than one operator such as AND (&&)
and OR (||)
operators.
These operators in a Linux command work like a ternary operator (condition ? expression_true ; expression_false).
So, if the first command1 (or condition) is true then other commands (command2 and command3) will execute command1 && command2 || command3
So, if we use first OR (||)
and then AND (&&)
operator in the above statement, in this case, if command1 fails then the other commands (command2 and command3) will execute on your system.
command1 || command2 && command3
For example, we want to search a file using the find
command that asks the shell to search for a specific file in the current working directory. If the file is not present, the terminal transfers the flow to the echo and, touch commands. In this way, it will create a new file in the current directory.
[ -f memo.txt ] || echo “File not found” && touch memo.txt
So, we can also do the above example, using the first AND (&&)
and then OR (||)
operator. For example, we want to search a file using the find command that asks the shell to search for a specific file in the current working directory.
The terminal transfers the flow to the echo
and touch
commands if the file is present. So, if the file is not present, it will display the specified message File not found.
[ -f memo.txt ] && echo “File not found” || touch memo.txt
Similarly, we can also use semicolon (;)
, AND (&&)
, and OR (||)
operators in the Linux bash script.
cd Downloads ; cat test.txt || touch text.txt
Conclusion
We discussed in this guide how to run multiple commands at once in Linux using different operators such as Semicolon (;)
, AND (&&)
, and OR (||)
. The use of each operator is quite different or opposite from each others.
Moreover, running multiple commands at once gives us more understanding of the flow of control of different Linux commands. I hope this tutorial will give you a clear understanding of all operators. Please feel free to ask any questions related to this article.