How to combine commands to execute one after another in Linux

Combine commands in Linux

When I first used Linux, software stores as such did not exist. There were package managers, like Synaptic, but nothing like GNOME Software, Discover, or Pamac. The first thing I learned was to update the Ubuntu repositories and then the packages. Later I got into the habit of also removing packages that were no longer needed, but this is not always recommended. I explain that because this article is about how to combine commands in linux.

There are three ways of doing it that are the most common. In one of them, all the commands will be executed regardless of whether one fails or not; the second, from the second, will only be executed if the previous one has worked; in the third one, one or the other will be executed. Each command or symbol between commands means one thing, and everything will be explained below.

Combine commands with the &&,; and ||

Having explained the above, let's first talk about the && operator. Although there are two "and" (and), it could be counted as one, but the second command it will only run if the first one has worked. For example, if we are on an operating system based on Debian / Ubuntu and we have added the official OBS Studio repository, to install it we will have to update the repositories and then install the OBS from the official source. The command would look like this:

sudo apt update && sudo apt install obs-studio

This means "update the repositories and, if we can update them, install OBS." If, for example, we do not have an internet connection and the repositories cannot be updated, the second command will not be executed.

The second is the semicolon operator. We will use this operator (;) if we want to execute several commands regardless of whether one fails or not. For example, if we have a large screen that allows it, we can write:

neofetch ; cpufetch

and in the same terminal window we will see information about the operating system, desktop, etc (neofetch), and CPU (cpufetch). If we misspell one of the two, the other will appear.

Finally, we have the two vertical bars (||) that mean "O", that is, either one or the other. If we have command_1 and command_2, we would write:

comando_1 || comando_2

and from the above, it would try to execute command_1. If it is unsuccessful it will go to command_2 and will continue until it reaches the end or one that works. For example, we can write cd Directory || mkdir Directory, with which either we will enter that directory or, if it does not exist, it will create it.

What if I want to combine more?

In one line we can combine more than two commands, and even the operators. For example:

comando_1 || comando_2 && comando_3

From the above, it will try to execute command 1. If it succeeds, it will stop. If not, it will execute command 2 and the third after, as long as command 2 succeeds.

And this is how commands are combined in Linux. All you have to do is remember what each operator means:

  • &&= y, if the above has worked.
  • || = or.
  • ;= everything.

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Juan said

    Hey can you put an option to download the articles in pdf as well as had masgnulinux? Thank you very much very useful article. By the way does anyone know what was Masgnulinux?

  2.   Hernán said

    Excellent, very useful.
    Many thanks friends.