Basic Operators in Linux

FREE Online Courses: Elevate Skills, Zero Cost. Enroll Now!

In this article, you will learn all there is to the control operators in Linux-based operating systems. We start from  fundamentals like what an operator is and why it is used and only then jump into what operators are. We will discuss the different types of operators along with an example of it in a small shell program. So pay attention and read right till the end.

What are operators?

If variables are the building blocks in computer science, operators are more like the cement that makes these variables meaningful and sometimes even build them into expressions. Operators are symbols that perform various different actions on variables.

Types of operators in linux

The types of operators are standard in all programming languages, the only thing that seldom changes is the operators themselves. Let us look at the types of operators:

1. Arithmetic Operators
2. Relational Operators
3. Boolean Operators
4. String Operators
5. File Test Operators

Now that we have laid down the fundamentals, let us look at each of these types in more detail.

Arithmetic operators in linux

As the name suggests, these operators help us in doing basic arithmetic problems, just like basic math problems consist of addition, division, subtraction, and multiplication (of course math is far more advanced like integrations, trigonometry, differentiation, and do much more!). Let us look at each of the arithmetic operators in detail with an example.

1. Addition operator (+)

The addition operators are the variables it is encased in between. It is denoted by a plus symbol (+).

2. Subtraction operator (-)

The subtraction operator subtracts the variables it is encased in between. It always subtracts the second number from the first number. It is denoted by a minus symbol (-).

3. Division operator (/)

The division operator divides the variables it is encased in between. It always divides the first number with the second number. It is denoted by a forward slash (/).

4. Multiplication operator (\*)

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

The multiplication operator divides the variables it is encased in between. It is denoted by a backslash followed by an asterisk(\*).

5. Modulus operator (%)

The modulus operator divides the variables and prints the remainder. It is denoted by a percentage symbol (%).

For Example: 10%3 will print 1 as output.

Relational operators in linux

As the name suggests, these operators are used to relate 2 variables, these operators come in very handy while checking conditions in loops. Let us look at each of the relational operators with examples.

1. Greater than operator (-gt)

The ‘greater than operator’ checks if the variable on the left is greater than the variable on the right or not. If it is greater, then the condition is true. It is denoted by “-gt”

2. Less than operator (-lt)

The ‘less than operator’ checks if the variable on the left is less than the variable on the right or not. If it is greater, then the condition is true. It is denoted by “-lt”

3. Greater than or equal to operator (-ge)

The “greater than or equal to” operator checks if the variable or the number on the left-hand side is greater than or equal to the variable or number to its right. It is denoted by “-ge”’

4. Less than or equal to operator (-le)

The “less than or equal to” operator checks if the variable or the number on the left-hand side is lesser than or equal to the variable or number to its right. It is denoted by “-le”.

5. Equal to operator (-eq)

The “equal to” operator checks if the variables it is encased between are equal or not. It is denoted by “-eq”

6. Not equal to operator (-ne)

The “not equal to” operator checks if the variables it is encased between are not equal. It is denoted by “-ne”

Logical operators in linux

Logical operators are slightly more intimidating, thankfully there are only a few of them, we will be mainly focusing on: “Logical and”, “Logical or” and “Logical negation (!)”. These club 2 or more relational statements to narrow down the condition even more. Let us look at how these work before we see an example.

1. Logical and operator (&&)

The “logical and operator” will be true if and only if both the conditions are true, it will be false in all other cases, meaning it will be false if any one or both of the conditions are false. It is denoted by 2 ampersand symbols (&&). To understand this operator better, let us look at the truth table enclosed with different scenarios:

NOCONDITION  1CONDITION 2CONDITION  1 && CONDITION  2
1TRUETRUETRUE
2TRUEFALSEFALSE
3FALSETRUEFALSE
4FALSEFALSEFALSE

2. Logical or operator (||)

The “logical or operator” will be true if any one of the conditions is true, and it will be false if and only if both the conditions are false. It is denoted by 2 pipe symbols (||). To understand this operator better, let us look at the truth table enclosed with different scenarios:

NOCONDITION  1CONDITION 2CONDITION  1 || CONDITION  2
1TRUETRUETRUE
2TRUEFALSETRUE
3FALSETRUETRUE
4FALSEFALSEFALSE

3. Logical negation (!)

This operator is pretty simple, it simply inverts the truth value, if it is true, it will become false and vice versa:

NOCONDITION  ! CONDITION 
1TRUEFALSE
1FALSETRUE

String operators in linux

Just like we have operators on numbers, we have operators on strings too! Let us first look at all 5 string operators before we look at examples of them cumulatively.

1. =
This string operator checks if the value of two operands are equal.

2. !=
This string operator checks if the value of two operands are not equal.

3. -z
This string operator checks if the given string operand size is zero

4. -n
This string operator checks if the given string operand size is non-zero

5. Str
This string operator checks if str is not an empty string

File test operators

As the name suggests, these operators test different conditions on files. For example, these operators can check if a file is executable, ordinary, readable, is a directory or not and so many more things. Let us look at each file operator along with an example for a few.

-b operator
This file test operator checks if a file is a block special file

-c operator
This file test operator checks if the file is a character special file.

-d operator
This file test operator checks if the file is a directory.

-f operator
This file test operator checks if the file is an ordinary file or if it is a directory or special file.

-g operator
This operator checks if the file has its set group ID (SGID) bit set.

-k operator
This file test operator checks if the file has its sticky bit set.

-p operator
This file test operator checks if the file is a named pipe.

-t operator
This operator checks if the file descriptor is open and associated with a terminal.

-u operator
This operator checks if the file has its Set User ID (SUID) bit set.

-r operator
This file test operator checks if the file is readable.

-w operator
This file test operator checks if the file is writable.

-x operator
This file test operator checks if the file is executable.

-s operator
This file test operator checks if the file has size greater than zero.

-e operator
This file test operator checks if the file exists.

Bitwise operators in linux

Bitwise operators are operators that perform bitwise operations on bit patterns. Let us look at each of the 6 bitwise operators along with an example.

1. Bitwise and

The bitwise and operator performs the “binary and” operation on bit pattern operands.

2. Bitwise or

The bitwise or operator performs the “binary or ” operation on bit pattern operands. It is denoted by a single ampersand symbol (&)

3. Bitwise Xor

The bitwise Xor operator performs the “binary exclusive or” operation on bit pattern operands. It is denoted by a single pipe symbol (|)

4. Bitwise complement

The bitwise complement operator performs the “binary not” operation on bit pattern operands. It is denoted by a wavy hyphen symbol (~).

5. Left shift

The “left operator” shifts the bits of the left operand, to the left by the number of times that is specified by the operand right. It is denoted by 2 less than symbols (<<).

6. Right shift

The “right operator” shifts the bits of the left operand, to the right by the number of times that is specified by the operand right. It is denoted by 2 greater than symbols (>>)

Summary

As you have seen, operators are the backbone of any programming language let alone shell programming. You have now learned what operators are and the different types of operators used in shell programming, like arithmetic, logical, relational, string operators and more.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

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