Chaining commands in Linux : how does it work ?
Table of Contents
I. Introduction
Under Linux, it's often necessary to execute several commands to accomplish a task. Sometimes, to increase efficiency, it may be useful to chain several commands together, so that they are executed one after the other. But how do you do this?
This tutorial presents the different ways of linking commands under Linux, using several examples. This will enable us, for example, to switch from the :
sudo apt-get update
sudo apt-get upgrade -y
To this one:
sudo apt-get update && sudo apt-get upgrade -y
Before you start, you should know that on Linux, there are several characters that act as operators and allow commands to be chained together:
- The semicolon ";"
- The double ampersand"&&".
- The double vertical bar"||"
- The"|" pipeline
Each character has a different behavior when it comes to linking commands, as we'll see later in this article.
II. Using the semicolon
On Linux, the semicolon can be used to chain several commands together, regardless of whether they succeed or fail. In this way, all commands are executed in sequence, even if a previous command fails. There is no control over the result of the previous command.
For example, if we want to create a folder, access that folder, then create an empty file, we'll chain three commands like this:
mkdir /home/flo/folder1/; cd /home/flo/folder1/; touch myfile.txt
Given the user's current permissions, we can assume that everything will go according to plan and that there are no real risks. Nevertheless, this method can be considered the least "clean" for chaining commands under Linux.
Indeed, on different and more complex commands, we can imagine that a command encounters a problem and returns an error. This would disrupt the execution of all subsequent commands! It would then be preferable to be able to execute subsequent commands only if the previous ones have been executed correctly...
III. Using the double ampersand
To solve the above problem, we can rely on the use of the double ampersand to sequence commands. If we take the previous example, the result is :
mkdir /home/flo/folder1/ && cd /home/flo/folder1/ && touch myfile.txt
Now, if the "cd /home/flo/dossier1" command fails, the touch command to create the file will also fail, as will the cd command. So there will be no further errors.
Instead of targeting the "/home/flo" directory to create "folder1", let's imagine that I mistakenly specify "/home/FLO". This is incorrect, as Linux is case-sensitive. If we run a sequence of commands with the semicolon, the first two commands will return an error, while the third (touch) will execute correctly. The problem is that, instead of creating the file in the "/home/flo/folder1" directory, it will create it in the current directory! If we perform the same test with the "&&" operator, we can see that execution is halted as a result of the error encountered by the mkdir command.

Under Linux, each command returns an output code that can be retrieved via the $? variable (as with PowerShell). An output code of 0 indicates success, while a code other than 0 (e.g. 1 or 2, depending on the case) signals an error. Linux uses these codes to determine whether the next command in a chain should be executed.
Here's an example:

Another common example is the sequence of commands for updating and upgrading packages on a Debian-based distribution:
sudo apt-get update && sudo apt-get upgrade -y
In this example, "sudo apt-get upgrade -y" will only be executed if the "sudo apt-get update" command succeeds.
We could also use this method to download an archive, extract it and then move to the resulting directory.
wget https://example.com/archive.tar.gz && tar zxvf archive.tar.gz -C /opt/ && cd /opt/archive/
In this example, each step depends on the success of the previous one:
- The wget command downloads the archive file.
- If the download is successful, tar extracts the archive into the /opt/ directory.
- Finally, if extraction is successful, cd changes the current directory to the extracted folder.
IV. Using the double vertical bar
The double vertical bar allows you to execute a command only if the previous command fails (output code other than 0).
cd /home/flo/dossier1/ || echo "This folder doesn't exist."
In this simple example, if the folder does not exist, the message "The folder does not exist" will be displayed in the console (in addition to the error returned by the command).
For example, we can try to access the "/home/flo/folder1" directory, and if it doesn't exist, we'll create it.
cd /home/flo/folder1 || mkdir /home/flo/folder1
You can also combine several operators to create more complex sequences. Here, the message "Error!" will be returned to the console if one of the two commands returns an error.
mkdir /home/flo/dossier1 && cd /home/flo/dossier1 || echo "Error !"
V. Using the pipeline
The pipeline is a widely used operator in scripting and programming languages, including Bash (and PowerShell). It allows you to redirect the standard output of a first command to the standard input of a second command (and so on). Although it's a little different from the command sequence we've just described, it's still important to know about it.
The example below displays only files with the ".txt" extension in the current directory, using a grep-based filter.
ls -l | grep ".txt"
VI. Conclusion
By using these operators, you can create efficient sequences to automate your Linux tasks. Of course, you can also use them in your Bash scripts! Feel free to test them yourself and apply them to your own needs.