In Linux, the terminal is the most crucial utility to perform tasks. Any task performed using GUI relies on terminal-based executions at the back end. Linux offers some extended functionalities to run multiple tasks or join the tasks. The “||” is an operator in Linux that is exercised to compare multiple commands and run the correct one. The working and usage of the “||” command in Linux will be covered in this write-up. The content covered in this write-up is as follows:
- What is || Command in Linux?
- How to Use || Command in Linux?
Let’s get started.
What is || Command in Linux?
OR (||) is the logical operator that returns true when any of the given commands is true. This operator will execute only one command, even if both commands are written correctly. In Linux, the || command is just like the else statement of programming, which is written between the commands. the following is the representation of the || command:
<command1> || <command2> ||.....<command n>
User can write the commands at both ends of the operator as shown in the following example:
$ echo linux || echo itslinuxfoss
The || command ( a logical operator) that works according to the OR logic Gate in which this operator returns true when any of the input is given true or 1, and returns false only if all the inputs are false or 0. The truth table of the logical OR operator is given below.
How to Use || Command in Linux?
This section enlists various examples of using the “||” command in Linux. Let’s get into them:
Example 1: Test Two Commands
The command provided below is exercised to show the usage of the “||” operator with two correct commands:
$ echo command1 || echo command2
If both commands are true, then only the first one will be executed:
The output shows that only the first command is executed.
Example 2: Bypass Incorrect Command
Here, an example scenario is presented where one of the two commands is false:
$ cd/ Downloads || ls
The output shows that the second command is executed, bypassing the first command.
“ls” command will execute because the First command is wrong in this scenario.
Example 3: Test Multiple Commands
In this case, we are executing || with multiple commands where two of three commands are wrong:
$ apt updat || mkdire tmp || echo ubuntu
In the above image, the “ls” command will be executed because the first two commands are wrong, the third and fourth command is true but will only execute the third command (first true occurrence).
Example 4: Using || With &&
User can also use the ||(OR) operator with &&(AND) operator. The && operator returns true when all the given commands are true else, false. To verify this, let’s understand it with an example.
In the following command, we use the “ping” and “echo” commands with && operator. These two commands are being used by the || operator with another “echo” command:
$ ping itslinuxfoss.com -c3 && echo "verified" || echo "Network Down"
The above command printed the “verified” on the screen after successfully executing the ping command because both commands returned true. It will not go to the || command.
Now, let’s check the above command output by disconnecting the internet connection:
Once any of the commands fail used in && operator, then the || command will be executed. That’s why “Network Down” is printed on the screen.
In Linux, the “||” command is used to check multiple commands at a time using the terminal. The “||” executes only the first (one) correct command that occurs in the pattern. This post has demonstrated all the possible uses of || commands in Linux with a detailed guide and explanation.