Bash Command Line Chain Operators in Linux with Examples

Last updated: July 12, 2022 | Bobbin Zachariah

Linux command chaining is a technique of combining several commands so that each of them can execute after the other based on the operator between them. The operator that separates these commands is the most important aspect of command line chaining. These operators control how these commands get executed. To reword, they control the flow of execution. Linux command chaining is very useful if you want to execute multiple commands at one goal.

Chaining Operators in Linux

Below are some commonly used chaining operators which you can use with different commands:

Chaining OperatorDescription
& (Ampersand)This send a command, script, or process to the background. In short, it makes a command run in the background.
&& (Logical AND)The && operator will only execute the second command if the first command SUCCEEDS! , in other words if the first command exists with a zero status.
|| (Logical OR)It's much more like an else statement in programming. The || will only execute the second command if the first command fails. In other words, if the first command exits with a none zero status code.
; (Semi-colon)The command following this operator will execute even if the command preceding this operator is not successfully executed.
! (NOT)The NOT is much like the except statement. It will run all the commands except a given condition. It negates an expression within a command.
&&-|| (AND-OR)It's a combination of the AND OR operator. It's much like the if-else statement in programming.
| (Pipe)The output of the command preceding this operator will act as an input of the command succeeding this operator. In other words the output of another command will be given to the input of the other command.
>,>>, < (Input-OutputRedirection)Redirects the output of a command or a group of commands to a file or stream.
\ (Concatenation)Used to concatenate large commands over several lines in the shell.
() (Precedence)Allows the commands to execute in precedence order.
{} (Combination)The execution of the command succeeding this operator will depend on the execution of the first command.

Bash chain commands practical examples

Now that you understand what command line chaining is, let's look at the most common operators that allow you to chain multiple commands together.

1. Ampersand (&)

The ampersand operator is used to run a command in the background. The ampersand operator is very useful in situations where you don't want to wait for a command to finish before doing anything else.

Here's an example of how to use the ampersand operator to send processes to the background:

$ subl
[1] 105811                                                                                                                                                                  $
[1]  + done       subl

The ampersand operator separates your bash commands from the shell and allows them to run in the background on your system.

The number in the square brackets in the preceding example is the job number assigned by the shell to the background process. Following is the PID (Process ID) of the process assigned to it by the system. You can also see that a new shell has been returned, and the command you executed will run in the background without any issues, allowing you to enter new bash commands at the prompt.

When the background process completes, the terminal displays the following message:

[1]  + done subl

This displays the job number and status (done), as well as the command used to initiate the job.



Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment