In this article, we explain what chaining operators in Linux are and give practical examples in Linux terminal on the frequently used 6 chaining operators.
What is Linux?
Linux is a family of open-source operating systems based on the Linux kernel. The first Linux system kernel was released on September 17, 1991, by Linus Torvalds.
There are also quite a number of customized Linux distributions, such as Kali Linux, REMnux etc. Kali Linux is a Debian-based distribution developed, funded and maintained by Offensive Security for ethical hackers for the purposes of Penetration Testing, Security Research & Assessment, and Computer Computer Forensics & Reverse Engineering. REMnux, on the other hand, is a Linux distro curated for reverse-engineering and malware analysis purposes.
UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.Dennis Ritchie
Read more educational and inspirational cyber quotes at our page 100+ Best Cyber Security & Hacker Quotes.
6 Useful Chaining Operators in Linux
Chaining operators in Linux are used to combine multiple commands and execute the commands based on the functionality of the operators used in between the commands. Chaining operators are very useful in that they enable users to run multiple commands on a single command line, instead of typing the commands one after another and waiting for the currently running command to get completed until the terminal allows for typing the following commands. Besides that, the behavior of the commands chained together is usually different than the commands run separately on multiple lines, depending on the semantics of the operators used. Chaining operators also come very handy in that they are like short shell scripts that can be run directly from the terminal.
Ampersand Operator (
& operator can be used to run a command in the background. This frees the terminal and allows users to run other additional commands in the same terminal while the earlier command still continues to run in the background. This is especially useful when a long running command is typed in the terminal. It is also useful when the typed command initiates a GUI (Graphical User Interface) for user to interact and blocks further CLI (Command Line Interface) interaction until the GUI is terminated.
You might have come across this operator before, but what you may not know is that you can execute multiple commands in the background on a single command line.
For instance, you can run two GUI applications (Wireshark and Firefox) simultaneously from the terminal with a single line of command and still continue to use the terminal for additional tasks.
$ wireshark & firefox https://google.com &
When this command is typed, an output like the one below is displayed on the Linux terminal.
The first column on this output shows the job numbers of the backgrounded processes while the second column indicates the PIDs (Process IDs). A backgrounded process can be foregrounded by the
fg command followed by a
% sign and the job number or it can be terminated by using the
kill command followed by the PID of the process.
$ fg %1
Semi-Colon Operator (
The simplest of the chaining operators, the Semi-Colon operator can be used just to combine multiple commands together in a single line and execute them sequentially.
For instance, in the following command chaining, the first command creates a new directory (
newdir) and the contents of another directory (
olddir) are copied over to this directory. With the second command, a sub-directory is created under the newly created directory. Note that the processing of the commands are sequential.
$ cp -r olddir newdir ; mkdir newdir/subdir
AND Operator (
The AND operator is very similar to the Semi-Colon except that the second command gets executed only if the first command executes successfully. It is very much analogous to the
if-then statement in programming.
For instance, in the above example,
newdir/subdir is created irrespective of the successful execution of the first command (
cp). However, in the following example,
newdir/subdir is created only if the contents of the
olddir are copied to the
$ cp -r olddir newdir && mkdir newdir/subdir
To give another example, the following command chaining can be used to make sure operating system upgrade takes place only if the repository is updated successfully.
$ sudo apt update && sudo apt -y full-upgrade
OR Operator (
The semantics of the OR operator is just the opposite of the AND operator. The second command following the OR operator gets executed only if the first commands fails. In programming, it is much like the
In the following example, an error log is appended to a log file only if the file copy command fails.
$ cp -r olddir newdir || echo "Copy command not succeeded!" >> logs.txt
PIPE Operator (
The PIPE operator is one of the most well-known and frequently used chaining operator in Linux. It allows for passing the standard output of the first command as standard input to the second command.
For instance, to print only the lines containing IPv4 addresses from output of the
$ ifconfig | grep -w inet
To edit a line of string output by the
$ echo "The examples are very useful" | sed 's/useful/helpful/g'
Our last chaining operator is used to indicate precedence over commands by grouping the commands together that contain the previously discussed operators.
For instance, in the below given example, if Command1 fails, Command2 does not get executed because of the AND relationship but the rest of the commands are executed due to the following OR operator.
$ Command1 && Command2 || Command3 && Command4
However, in the following example, none of the following commands are executed if the first command fails since Command2 and Command3 are evaluated as a single command and thus the OR operator is disregarded.
$ Command1 && (Command2 || Command3) && Command4)
A Practical Example
Finally, for a more practical, let’s write a command chain that searches for a given file by using the
locate command and prints on the terminal whether it has found or not the search item.