Jannah Theme License is not validated, Go to the theme options page to validate the license, You need a single license for each domain name.
Linux

Top 10 Useful Chaining Operators in Linux with Examples

An operator used in between several commands allows you to link them in a chain that executes them based on the behavior of the operator used between them. A Linux command chain is similar to writing shell scripts directly from the command line. Automating them is therefore possible. Chaining operators can also help an unattended machine function more efficiently.

This article will shed light on commonly used command-chain linux operator, with brief descriptions and examples of linux & operator terminal that will surely increase your productivity and let you write shorter, more meaningful codes while reducing some system load.

Chaining Commands in Linux

In Linux, we can chain commands to execute multiple commands simultaneously and directly from the terminal. This is similar to short shell scripts you can execute directly through the terminal. A Linux command chaining technique consists of merging several commands into one that will execute in sequence depending on the operators that separate them, and these operators determine how the commands will be executed by  logical operators in shell script. The technique can run several commands at the same time.

Chaining with chaining operators

1. Ampersand (&) Operator:

 It is used to execute a command in the background without interrupting other commands. Processes/scripts/commands are sent into the background to execute other commands in the foreground. Script execution speeds up and the operator better utilizes system resources in shell script. Other programming languages refer to this as creating a child process or forking. Ampersands can be used in the following ways:

ping -cl google.com & #change the command before &
ping -c1 google.com & ping -c1 geeksforgeeks.org &

2. AND (&&) Operator:

 A command following this operator will only be executed if the preceding command is successful. If the first command has been executed successfully, it is useful to execute another command after it has been successfully executed.

echo "hello there" && echo "this is gfg"
apt update && echo "hello"
AND chaining operator

3. Semi-colon(;) Operator:

 In this way, multiple commands can be executed sequentially in one go. Nevertheless, it is crucial to note that the commands chained by the (;) operator are always executed sequentially. Separating two commands by the operator will always cause the second command to execute independently of the first. The second command is executed independently of the exit status of the first command, unlike the ampersand operator. The second command will always be executed even if the first command fails, i.e., the exit status is not zero.

who;pwd;ls

semicolon chaining operator

Sequentially, each command will be executed. Regardless of whether the execution of the first command is successful, the command following the ( ; ) operator will still execute.

4. Logical OR (||) Operator:

The command following this operator will only be executed if the command preceding it has failed. That is, it is the same as an else statement. The second command will be executed if the first command’s execution status is not zero.

echo "hello there" || echo "This is gfg"
apt update || echo "hello"
or chaining operator

5. Piping (|) Operator: 

The output of the first command is sent to the input of the second command with this operator.

ls -l | wc -l

The wc -l command in the above command displays the number of lines. ls -l displays the lists the files in the system.  This command displays how many files reside in the directory. The output of ls – l is passed to the next command, which counts the lines. As a result, we can find out the number of files in a directory by using pipe.

piping operator

6. NOT (!) operator:

 When used in command/, it negates an expression. Using this, all but one file in a directory is deleted.

touch a.txt b.txt c.txt d.txt e.txt
rm -r !(a.txt)

A.txt is the only file that will be removed from the directory with this command. The image below shows how we created some files inside a directory using the touch command. Directory listing shows what files are present. The use! command deletes all files except a.txt. The operator. A.txt is the only file that has been removed, as we can see if we list the files again.

NOT operator

7. Redirection Operators(‘<‘,’>’,’>>’):

 A group of commands or a group of commands can be redirected to a stream or file using this operator. Standard input and output can be redirected with this operator. Redirection operators are supported by almost all commands.

cat >>file_name
sort <file_name

With the first command we create a file named “file_name” (with the redirection operator >> we can input information into the file) and then with the second command we sort the contents of that file. As you can see in the image below, we must first create a file with numbers before using this command. The contents are then sorted.

8. AND, OR Operators as an if-else condition: 

If-else statements can be used with this command. These are logical ANDs and logical ORs.

[ ! -d ABC ] && mkdir ABC || cd ABC

First, this command will determine whether the directory ‘ABC’ exists or not. Otherwise, a new directory will be created if it doesn’t already exist, otherwise, ‘ABC’ will become the current directory. If you look at the image below, we create the directory ‘ABC’, as it doesn’t exist. The directory already exists the second time the command is run, so ‘ABC’ becomes the current directory.

9. Concatenation Operator(\): 

 A shell command that concatenates several commands into one. The result is easier to read. It is used to execute large commands because a large command is split across multiple lines.

10. Precedence: 

To execute multiple commands in a specified order, this command sets the precedent value.

The first command will be executed if it is successful, but the second command will not be executed. In the second case, however, the third command will be executed since the () operator is used to set precedence. Refer to the following image: If the directory already exists (the first command), then the current directory becomes PQR (the second command), but in the first case the third command does not get executed, but in the second case, the third command is executed.

Also Read: Learn to use SCP Command in Linux (with Examples)

Conclusion

Throughout this tutorial, we’ve seen two common ways to chain commands by shell script operators bash: inline and waiting for other processes to finish. You can also chain commands through variables. The possibilities are endless in operator shell scripting operators. We can even Use user-defined functions, script files, conditionals, or loops to implement these techniques.

Arpit Saini

He is the Director of Cloud Operations at Serverwala and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles