We’ll learn about how to execute several commands simultaneously in Linux in this article. Every operator has its own advantages when it comes to separating commands. This tutorial will help a little bit in improving how we execute commands and author shell scripts.
The Linux operating system offers a simple command line interface for managing the system. There are shells such as Bash, CSH, and Zsh that accept commands from the user and route them to the kernel. A command is used to perform some function on the system. We may also specify multiple shells at once and execute them one after the other.
There are three distinct options available using the separator operators. In the following section, we will look at them in detail.
|Semicolon (;)||command1; command2||Run both commands one by one|
|Logical AND (&&)||command1 && commnd2||Run command2 only if command1 is successfull|
|Logical OR (||)||command1 || command2||Run command2 only if commadn1 failed|
Let’s discuss all the options in detail.
Using Semicolon (;)
Semicolons (;) separate commands to guarantee that subsequent commands run regardless of the previous ones’ exit statuses. Use this option to ensure that command runs after the completion of the previous one.
command1; command2; commandN
date; pwd; whoamiSat Aug 6 01:56:05 UTC 2022 /home/rahul rahul
Even though the second command fails because of a permissions error, the third command still executes in the following commands:
date; touch /root/a.txt; whoamiSat Aug 6 01:59:31 UTC 2022 touch: cannot touch '/root/a.txt': Permission denied rahul
Using Logical AND Operator (&&)
Upon successful execution of the previous command, the next command will also run. The logical AND (&&) operator checks for the exit status of the previous command.
However, if the previous command finished with non-zero exit status, the execution will stop here. No subsequent commands will run in that case
command1 && command2 && commandN
mkdir ./backups && cd ./backups
The last command will not run if the first command failed due to any reason:
mkdir /root/backups && cd /root/backupsmkdir: cannot create directory ‘/root/backups’: Permission denied
Using Logical OR Operator (||)
The logical OR (||) condition checks for the exit status of the previous command and executes the next command only if the previous command failed.
command1 || command2 || commandN
You can use this construct in shell scripts to determine whether a file or command is available. For instance, in a backup script, you can check whether
/usr/bin/mysqldump exists or not, and if not, you can print a message or terminate the process.
[ -s /usr/bin/mysqldump ] || echo "command not found"
Use this to test command or file that isn’t on your system. This is useful for bash scripts that create files if they are missing. You can also stop script execution if required files are missing.
[ -s /usr/bin/not_a_cmd ] || echo "command not found"
In this article, we’ll go over how to run multiple commands simultaneously in Linux. We’ll also cover the various operators used to separate commands from one another. Each operator affects the way a command is executed, and each has its own benefits. This tutorial will provide some useful information to anyone interested in enhancing their command-running or shell-script-writing skills.