10 Useful Chaining Operators in Linux with Examples

Chaining of Linux commands means, combining several commands and making them execute based upon the behavior of the operator used in between them.

Chaining of commands in Linux is something like you are writing short shell scripts at the shell itself, and executing them from the terminal directly. Chaining makes it possible to automate the process.

Moreover, an unattended machine can function systematically with the help of chaining operators.

This article aims to shed light on frequently used command-chaining operators. It provides short descriptions and corresponding examples that can enhance your productivity and enable you to write concise, meaningful code while reducing system load at times.

1. Ampersand Operator (&) – Run Linux Command in Background

The function of ‘&‘ is to make the command run in the background. Just type the command followed by a white space and ‘&‘. You can execute more than one command in the background, in a single go.

Run a single Linux command called ‘ping‘ in the background:

ping -­c5 www.tecmint.com &

Run two or multiple apt commands in the background, simultaneously:

apt update & apt upgrade &

2. Semi-Colon Operator (;) – Run Multiple Commands

The semi-colon (;) operator makes it possible to run, several commands in a single go and the execution of the command occurs sequentially.

apt update ; apt upgrade ; mkdir test

The above command combination will first execute the update instruction, then the upgrade instruction, and finally will create a ‘test’ directory under the current working directory.

3. AND Operator (&&) – Run Second Command Upon Success

The AND Operator (&&) would execute the second command only, if the execution of the first command SUCCEEDS, i.e., the exit status of the first command is 0. This command is very useful in checking the execution status of the last command.

For example, I want to visit the website tecmint.com using the links command, in the terminal but before that, I need to check if the host is live or not.

ping -c3 www.tecmint.com && links www.tecmint.com

4. OR Operator (||) – Conditional Command Execution

The OR Operator (||) is much like an ‘else‘ statement in programming. The || operator allows you to execute the second command only if the execution of the first command fails, i.e., the exit status of the first command is ‘1‘.

For example, I want to execute ‘apt update‘ from a non-root account and if the first command fails, then the second ‘links www.tecmint.com‘ command will execute.

apt update || links tecmint.com

In the above command, since the user was not allowed to update the system, it means that the exit status of the first command is ‘1’ and hence the last command ‘links tecmint.com‘ gets executed.

What if the first command is executed successfully, with an exit status ‘0‘? Obviously! The second command won’t execute.

mkdir test || links tecmint.com

Here, the user creates a folder ‘test‘ in his home directory, for which the user is permitted. The command executed successfully giving an exit status ‘0‘ and hence the last part of the command is not executed.

5. NOT Operator (!) – Selective Execution of Commands

The NOT Operator (!) is much like an ‘except‘ statement. This command will execute all except the condition provided. To understand this, create a directory ‘tecmint‘ in your home directory and ‘cd‘ to it.

mkdir tecmint 
cd tecmint

Next, create several types of files in the folder ‘tecmint‘.

touch a.doc b.doc a.pdf b.pdf a.xml b.xml a.html b.html

See we’ve created all the new files within the folder ‘tecmint‘.

ls 

a.doc  a.html  a.pdf  a.xml  b.doc  b.html  b.pdf  b.xml

Now delete all the files except the ‘html‘ file all at once, in a smart way using the rm command.

rm -r !(*.html)

Just to verify, last execution. List all of the available files using ls command.

ls 

a.html  b.html

6. AND – OR operator (&& – ||) – Conditional Execution of Commands

The above operator is a combination of ‘AND‘ and ‘OR‘ Operator. It is much like an ‘if-else‘ statement.

For example, let’s do a ping to tecmint.com, if success echo ‘Verified‘ else echo ‘Host Down‘.

ping -c3 www.tecmint.com && echo "Verified" || echo "Host Down"

Sample Output:

PING www.tecmint.com (212.71.234.61) 56(84) bytes of data. 
64 bytes from www.tecmint.com (212.71.234.61): icmp_req=1 ttl=55 time=216 ms 
64 bytes from www.tecmint.com (212.71.234.61): icmp_req=2 ttl=55 time=224 ms 
64 bytes from www.tecmint.com (212.71.234.61): icmp_req=3 ttl=55 time=226 ms 

--- www.tecmint.com ping statistics --- 
3 packets transmitted, 3 received, 0% packet loss, time 2001ms 
rtt min/avg/max/mdev = 216.960/222.789/226.423/4.199 ms 
Verified

Now, disconnect your internet connection, and try the same command again.

ping -c3 www.tecmint.com && echo "verified" || echo "Host Down"

Sample Output:

ping: unknown host www.tecmint.com 
Host Down

7. PIPE Operator (|) – Streamlined Output Processing

This PIPE operator is very useful where the output of the first command acts as an input to the second command. For example, pipeline the output of ‘ls -l‘ to ‘less‘ and see the output of the command.

ls -l | less

drwx------  tecmint tecmint  4.0 KB Thu Nov 16 12:03:02 2023 AnyDesk
drwxrwxr-x  tecmint tecmint  4.0 KB Tue Oct 10 10:44:35 2023 bin
drwxr-xr-x  root    root     4.0 KB Wed Nov 24 22:05:09 2021 DEBIAN
drwxrwxr-x  tecmint tecmint  4.0 KB Sat Nov 18 12:32:49 2023 Desktop
drwx--x---+ tecmint tecmint  100 KB Fri Dec  8 12:00:49 2023 Downloads
drwxrwxr-x  tecmint tecmint  4.0 KB Wed Sep 13 10:25:07 2023 llpp
drwxrwxr-x  tecmint tecmint  4.0 KB Thu Nov 16 14:26:46 2023 MyImages
drwxrwxr-x  tecmint tecmint  4.0 KB Wed Sep 13 14:55:42 2023 Obsidian Vault
drwxrwxr-x  tecmint tecmint  4.0 KB Tue Oct 10 10:44:32 2023 opt
drwxrwxr-x  tecmint tecmint  4.0 KB Thu Nov 16 14:46:54 2023 php
drwxrwxr-x  tecmint tecmint  4.0 KB Thu Nov 16 15:54:33 2023 tar_examples
.rw-rw-r--  tecmint tecmint   73 KB Mon Sep 18 11:46:02 2023 Apache-Web-Server-Security.png
.rw-rw-r--  tecmint tecmint  2.2 KB Sun Oct 29 00:17:15 2023 ChatGPT-SEO-Prompts
.rw-rw-r--  tecmint tecmint   41 KB Thu Nov 23 11:34:33 2023 Create-Users-in-Linux.png
...

8. Command Combination Operator {}

Combine two or more commands, the second command depends upon the execution of the first command.

For example, check if a directory ‘bin‘ is available or not, and output the corresponding output.

[ -d bin ] || { echo Directory does not exist, creating directory now.; mkdir bin; } && echo Directory exists.

9. Precedence Operator () – Managing Command Execution Order

The () operator makes it possible to execute commands in precedence order.

Command_x1 &&Command_x2 || Command_x3 && Command_x4.

In the above pseudo command, what if the Command_x1 fails? Neither of the Command_x2, Command_x3, Command_x4 would executed, for this we use Precedence Operator, as:

(Command_x1 &&Command_x2) || (Command_x3 && Command_x4)

In the above pseudo command, if Command_x1 fails, Command_x2 also fails but Still Command_x3 and Command_x4 execute depending upon the exit status of Command_x3.

10. Concatenation Operator () – Multiline Command Concatenation

The Concatenation Operator () as the name specifies, is used to concatenate large commands over several lines in the shell. For example, The below command will open a text file test(1).txt.

nano test(1).txt

That’s all for now. I am coming up with another interesting article very soon. Till then Stay tuned, healthy, and connected to Tecmint. Don’t forget to give your Valuable feedback in our comment section.

Similar Posts