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. 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. Syntax: Example: Even though the second command fails because of a permissions error, the third command still executes in the following commands:
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 Syntax: Example: The last command will not run if the first command failed due to any reason:
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. Syntax: 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. 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.
Conclusion
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.