The 10 most useful concatenation operators in Linux with examples

An operator used between multiple 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, so they can be automated. Chaining operators can also help an unattended machine run more efficiently.

This article highlights commonly used command chains Linux Operatorwith short descriptions and examples of Linux and operator terminal This will definitely increase your productivity and allow you to write shorter, more meaningful code while reducing the system load somewhat.

Chaining commands in Linux

Under Linuxwe can chain commands to run multiple commands at once and directly from the terminal. This is similar to short shell scripts that you can run directly from the terminal. A Linux command chaining technique is to combine multiple commands into one that is executed one after the other depending on the operators that separate them. These operators determine how the commands are executed. logical operators in shell scriptThe technology can execute several commands simultaneously.

Concatenation with concatenation operators

1. Ampersand (&):

It is used to execute a command in the background without interrupting other commands. Processes/scripts/commands are sent to the background to execute other commands in the foreground. Script execution is made faster and the operator makes better use of system resources in the shell script. In other programming languages, this is called 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 is only executed if the previous command was successful. If the first command was executed successfully, it makes sense 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. Semicolon(;) operator:

This allows multiple commands to be executed sequentially in one pass. Nevertheless, it is important to note that the commands chained by the (;) operator are always executed sequentially. When two commands are separated by the operator, the second command is always executed independently of the first. The second command is executed regardless of the exit status of the first command, unlike the ampersand operator. The second command is always executed even if the first command fails, that is, the exit status is nonzero.

who;pwd;ls

Semicolon concatenation operator

Each command is executed one after the other. Regardless of whether the first command is executed successfully, the command after the operator ( ; ) is still executed.

4. Logical OR operator (||):

The command following this operator is executed only if the previous command failed. That is, it is the same as an else statement. The second command is executed if the execution status of the first command is nonzero.

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

5. Piping operator (|):

This operator sends the output of the first command to the input of the second command.

ls -l | wc -l

The wc -l command in the above command displays the number of lines. ls -l displays the lists of files in the system. This command shows how many files are 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 using a pipe.

Pipe fitter

6. NOT (!) Operator:

When used in command/, an expression is negated, which deletes all but one file in a directory.

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

A.txt is the only file that is removed from the directory using this command. The image below shows how we created some files in a directory using the “touch” command. The directory listing shows what files are there. The “use!” command deletes all files except a.txt. The operator . A.txt is the only file that was removed, as we can see when we list the files again.

NOT operator

7. Redirection operators('<','>','>>'):

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

cat >>file_name
sort 

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

8. AND, OR operators as if-else condition:

This command allows you to use if-else statements, which 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, it will create a new directory if it does not exist yet, otherwise “ABC” will become the current directory. If you look at the image below, we are creating the directory “ABC” because it does not exist. The directory already exists when the command is executed for the second time, so “ABC” will become the current directory.

9. Concatenation operator(\):

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

10. Priority:

To execute multiple commands in a specific order, this command sets the precedence value.

The first command will be executed if it succeeds but the second command will not. However, in the second case, the third command will be executed because the () operator is used to set the priority. See the following picture: If the directory already exists (the first command), the current directory becomes PQR (the second command), but in the first case, the third command will not be executed but in the second case, the third command will be executed.

Also read: Learn how to use SCP command in Linux (with examples)

Diploma

In this tutorial, we learned two common ways to chain commands: Shell script operators Bash: inline and waits for other processes to complete. You can also chain commands using variables. The possibilities are endless in Operator Shell scripting operators. We can even use custom functions, script files, conditionals or loops to implement these techniques.

About the Author
Cristina Shank
Cristina Shank is a skilled Database Engineer with a degree from Stanford University. She specializes in optimizing and managing complex database systems, bringing a blend of technical expertise and innovative solutions to her work. Cristina is dedicated to advancing data management practices and frequently shares her insights through writing and speaking engagements.