Chaining Commands in Linux

14 Oct, 2020
Chaining commands in Linux allows us to execute multiple commands at the same time and directly through the terminal. It’s like short shell scripts that can be executed through the terminal directly. Linux command chaining is a technique of merging several commands such that each of them can execute in sequence depending on the operator that separates them and these operators decide how the commands will get executed. It allows us to run multiple commands in succession or simultaneously.
Some commonly used chaining operators are as follows:
Operators
Function
1. Ampersand(&) Operator: It is used to run a command in the background so that other commands can be executed. It sends a process/script/command to the background so that other commands can be executed in the foreground. It increases the effective utilization of system resources and speeds up the script execution. This is also called as Child process creation or forking in other programming languages. Ampersand sign can be used as follows:
ping -cl google.com & #change the command before &
ping -c1 google.com & ping -c1 geeksforgeeks.org &

2. AND (&&) Operator: The command succeeding this operator will only execute if the command preceding it gets successfully executed . It is helpful when we want to execute a command if the first command has executed successfully.
echo "hello there" && echo "this is gfg"
apt update && echo "hello"

3. Semi-colon(;) Operator: It is used to run multiple commands sequentially in a single go. But it is important to note that the commands chained by (;) operator always executes sequentially. If two commands are separated by the operator, then the second command will always execute independently of the exit status of the first command. Unlike the ampersand operator, the execution of the second command is independent of the exit status of the first command. Even if the first command does not get successfully executed i.e, the exit status is non-zero, the second command will always execute.
who;pwd;ls

The three commands will get executed sequentially. But the execution of the command preceding ( ; ) operator will execute even if the execution of the first command is not successful.
4. Logical OR (||) Operator: The command succeeding this operator will only execute if the execution of the command preceding it fails. It is much like an else statement. If the execution status of the first command is non-zero then the second command will get executed.
echo "hello there" || echo "This is gfg"
apt update || echo "hello"

5. Piping (|) Operator: This operator sends the output of the first command to the input of the second command.
ls -l | wc -l

In the above command wc -l displays the number of lines. ls -l displays the lists the files in the system.  The first command displays the number of files in the directory. ls – l lists the names of the files and this output is sent to the next command which counts the number of lines in the input. As a result, by using pipe we get the number of files in the directory.
6. NOT (!) operator: It is used to negate an expression in command/. We can use it to delete all files except one in a directory.
touch a.txt b.txt c.txt d.txt e.txt
rm -r !(a.txt)

This command will remove all files in the directory except  a.txt. In the image below, we create some files using the touch command inside a directory. ls shows the files in the directory. To delete all files except ‘a.txt’ we use! Operator. If we list the files again, we can see that all files except a.txt are removed.
7. Redirection Operators(‘<‘,’>’,’>>’): This operator is used to redirect the output of a command or a group of commands to a stream or file. This operator can be used to redirect either standard input or standard output or both. Almost all commands accept input with redirection operators.
cat >>file_name
sort <file_name

The first command creates a file with the name ‘file_name’  (The redirection operator >> allows us to give input in the file) while the second command will sort the contents of the file. Refer to the image below, we first create a file with numbers and then use this command. This sorts the content of the files.
8. AND, OR Operators as an if-else condition: This command can be used as an if-else statement. It is a combination of logical AND and logical OR operators.
[ ! -d ABC ] && mkdir ABC || cd ABC

This command will first check if the directory ‘ABC’ exists or not. If it does not exist then a new directory is created else ‘ABC’ becomes the current directory. Refer to the image below, the directory named ‘ABC’ does not exist and hence it is created. When the same command is executed the second time, then the directory already exists and hence ‘ABC’ becomes the current directory.
9. Concatenation Operator(\):  Used to concatenate large commands over several lines in a shell. It also improves the readability for the users. A large command is split over several lines and hence, it is used to execute large commands.
gedit text\(1\).txt

It will open a file named text(1).
10. Precedence: This command is used to set precedent value so that multiple commands can execute in a given order.
cmd1 && cmd 2 || cmd3
( cmd1 && cmd 2 ) || cmd3

In the first case, if the first command is successful then the second will get executed but the third command will not execute. But in the second case, the third command will get executed as the precedence is set using the () operator. Refer to the image below: If the directory exists (the first command), then the current directory becomes PQR (second command) but in the first case the third command is not getting executed while in the second case when the precedence operator is used then the third command is also executed.
11. Combination Operator ({}): The execution of the command succeeding this operator depends on the execution of the first command. The set of commands combined using {} operator executes when the command preceding it has successfully executed.
[ -f hello.txt ] && echo "file exists" ; echo "hello"
[ -f hello.txt ] && { echo "file exists" ; echo "hello"; }

In the first case, hello will always get printed. If the file exists then the command will get executed as it is preceding the && operator. If we want to execute both second and third commands only if the file exists, then we use {} operators to combine the commands.
linux-command
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.