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
; operator is the simplest of all. The commands separated by a semicolon are executed sequentially.
The syntax of this command is:
command-2 is executed whether or not
command-1 runs successfully, irrespective of the output or exit status. Upon the end of
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
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-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
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:
The or (||) operator
We can understand the 'or' operator like this, for the given syntax:
"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
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"
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.
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:
I hope you will find this guide helpful.