Updated March 18, 2023
Introduction to Linux Operators
One of the most widely used and recommended Computer Operating System for Development Purposes, “Linux”, comes with a wide range of execution commands. These commands are single line instructions executed on Terminal. There is a wide range of these commands, distinguished for a specific use. These commands are executed on Linux CLI (Command Line Interface). In this topic, we are going to learn about Linux Operators.
If you are new to Linux CLI and have no idea of these operators or how to use them, simply open up the terminal and fire “help” command, which will list down Linux Shell’s built-in commands with syntax.
One of such powerful operator is, ‘&&’ (double ampersand), which is a type of Chain Command and when used, executes the second inline command, upon the successful execution of the first command.
Example: “apt-get update && apt-get upgrade”, hereupon successful execution of update command, Upgrade command is executed.
Let’s dive into the world of unlimited Linux operators. To start with, Linux, like any programming language has a basic range of Operators.
Following is the basic classification or operators:
- Arithmetic Operators: used for the execution of mathematical operations.
- Relational Operators: simply defines the relation between two operands.
- Boolean Operators: also known as Logical Operators, these are used to perform logical operations.
- Bitwise Operators: “&, |, <<, >>”, used for simple bitwise operations.
- File Test Operators: plays with properties of a file.
List of Linux Operators
These are a few of the categories for operators. Other than this, Linux offers a wide range of commands. These operators basically are executed on a Bourne Shell. Bourne Shell is UNIX’s original command execution program, developed at AT&T, by Stephen Bourne, and thus the name. Now, let’s understand each of these operators in-depth with examples. For further examples, we will assume two variables X and Y, with values as 5 and 10.
1. Arithmetic Operators
Basically, these operators are used in simple mathematical calculations like addition and multiplications.
We have 7 Arithmetic Operators as follow:
- Addition (+): Simply adds values of the two given variables. Ex. X + Y will result in 15.
- Subtraction (-): Subtracts the value of one variable with the other. Ex. Y – X will result in 5.
- Division (/): Divides the two variables. Ex. X / Y will give us 0.5.
- Multiplication (*): Multiplies the two variables. Ex. X * Y will result in 50.
- Increment Operator (++): Simply adds one to the value. The increment operator works in two ways, as a prefix, and as a postfix, and based on the operator position, results may vary.
- For Prefix: with the operator before variable, Y++, it will return the value before incrementing, Ex. Y++ = 5
- For Postfix: with variable before operator, ++Y, it’ll return the incremented value, Ex. ++Y = 6.
- Decrement Operator (–): Similar to Increment, except this one deletes a single value. Works in two ways.
- For Prefix: First gives the value of Y then does the decrement operation, Ex. Y—will result in 5.
- For Postfix: initially decreases the value by one then gives the result, Ex. –Y will return 4.
Above are the arithmetic operators widely used, and now let’s begin with the second list of operators, i.e. Relation Operators.
2. Relational Operators
Simply returns either “true or false”, depending on the relation between the variables and are supported by Bourne Shell.
Below is the total of 6 types of Relational Operators:
- Equal to (==): Compares the two variables and returns true if they are equal and false if otherwise. Ex. X == Y will result in false.
- Not equal to (!=): similar to the equal to, except it returns true if the values are not the same and false if otherwise. Ex. X != Y will return true.
- Less than (<): if the left-hand value is less than the right-hand value, it returns true else false. Ex. X < Y will return true.
- Greater than (>): returns true if the left-hand value is greater than the right-hand value, else false. Ex. X > Y will return false.
- Less than or equal to (<=): true if the left-hand value is less than or equal to the right-hand value.
- Greater than or equal to (>=): true if the left-hand value is greater than or equal to the right-hand value.
Now that we have understood Relational and Arithmetic Operators, Let’s study the boolean and bitwise operators.
3. Boolean Operators
Totally supported on Bourne Shell, Boolean Operators are used in combination for better search results. AND, OR and NOT are the boolean operators and the use of these operator’s results is time-saving.
- AND: to narrow the search. Ex. Test marks AND final marks.
- OR: widen the search. Ex. Free courses OR under Rs.499.
- NOT: to eliminate keyword(s). Ex. Latest Additions NOT above Rs.499.
4. Bitwise Operators
Somewhat similar to Logical Operators, except Bitwise operators work on the lower level at Binary Representation of Data. Here, both the variables (right and left-hand value) must be Integer.
Below are the 6 Bitwise Operators.
- AND (&): comparison between two bits. Returns 1 if both bits are 1, else 0.
- OR (|): compares two bits and returns 1 if the bits are complementary, else 0.
- XOR (^): EXCLUSIVE-OR, similar to above operators, it compares both bits and returns 1 if any of the bit is 1. Else it gives 0 if both bits are 0 or 1.
- Compliment (~):
- Left Shift (<<): simply moves the bits to the right and a 0 is assigned to the leftmost.
- Right Shift (>>): just like SHIFT RIGHT but in the opposite way. It moves the bits to the left and assigns 0 to the rightmost.
Bitwise operators are used to alter an individual bit. And File Test Operators are as simple as operators to check for the properties of a file or the permissions like Read, Write and execute. These operators are used along with the IF clause.
To be Noted: you might have encountered how the passwords aren’t seen on the terminal as you type. To be clear, this is not an issue but a good security point. As you type in the password, it is kept in dark but Linux saves every tap.
Conclusion
In this article, we have covered the most widely used basic Linux Operators. From Arithmetic to Bitwise operators, with a brief explanation and an example. These are just the fraction of the commands available, Linux has a wide range of operator commands, you can learn and use commands as per your need. To use and harness the power of these commands will make operating Linux OS way easier.
Recommended Articles
This is a guide to Linux Operators. Here we discuss the List of Linux Operators which are basically executed on a Bourne Shell. You may also look at the following article.