Operators in Bash Shell Programming
Contents
Photo by NASA on Unsplash
If you want to learn bash bash scripting please read this article.
Learn complete Linux bash (shell) scripting in one article
Operators help us perform various types of operations such as addition, multiplication etc.
There are following types of operators present in bash:
- Arithmetic,
- Relational,
- Boolean,
- File Test,
- String Test
1. Arithmetic Operators
All the arithmetic operators present in bash are discussed below with examples.
These operators work with integers.
1.1 Addition Operator
It’s a +
that adds operands together.
Shown in the below example:
|
|
This will display:
50
NOTE:
- The
expr
command evaluates the expression.- There is
<space>
before and after the+
operator .
1.2 Subtraction Operator
It’s a -
that subtracts second operand from first operand.
Shown in the below example:
|
|
This will display:
10
1.3 Multiplication Operator
It’s a *
that multplies operands together.
Shown in the below example:
|
|
This will display:
6
1.4 Division Operator
It’s a /
that performs division operation.
Shown in the below example:
-Example 1-
|
|
This will display:
4
-Example 2-
|
|
This will display:
6
1.5 Modulus Operator
It’s a %
that performs modulus operation and returns the remainder.
Shown in the below example:
|
|
This will display:
1
1.6 Equality Check Operator
It’s a ==
that performs equality check operation and returns 1 if both sides are equal otherwise returns 0.
Shown in the below example:
-Example 1-
|
|
This will display:
1
-Example 2-
|
|
This will display:
0
1.7 Not Equal Check Operator
It’s a !=
that performs equality check operation and returns 0 if both sides are equal otherwise returns 1.
Shown in the below example:
-Example 1-
|
|
This will display:
0
-Example 2-
|
|
This will display:
1
2. Relational Operators
Relational operators are used to perform comparison among given operands.
-eq (Equals to operator)
This checks whether the operands are equal.
This operator is used to perform comparison in the if
condition.
|
|
Equal
-ne (Not equals to operator)
This checks whether the operands are equal.
This operator is used to perform comparison in the if
condition.
|
|
Not Equal
-gt (Greater than operator)
This checks whether the first operand is greater than the second operand.
This operator is used to perform comparison in the if
condition.
|
|
40 is greater than 20
-lt (Less than operator)
This checks whether the first operand is less than the second operand.
This operator is used to perform comparison in the if
condition.
|
|
20 is greater than 40
-ge (Greater than or equals to operator)
This checks whether the first operand is greater than or equals to the second operand.
This operator is used to perform comparison in the if
condition.
|
|
40 is greater than or equals to 20
-le (Less than or equals to operator)
This checks whether the first operand is greater than or equals to the second operand.
This operator is used to perform comparison in the if
condition.
|
|
40 is less than or equals to 200
3. Boolean Operators
! (Not operator)
This changes true
into false
and vice versa.
We have used this operator in Not equal operator under arithmetic operators.
Shown in the below example:
-Example 1-
|
|
This will display:
0
-Example 2-
|
|
This will display:
1
-o (OR operator)
Checks whether one of the operands is true and executes the statements.
|
|
40 or 10 is greater than 20
-a (AND operator)
Checks whether all of the operands are true and executes the statements.
|
|
40 and 100 are greater than 20
4. String Test Operators
These operators are used to perform string related comparisons.
= (Equality Operator)
Check whether two strings are equal.
|
|
User is allowed
!= (Not equal operator)
Check whether two strings are not equal.
|
|
User is not allowed
-z (Zero length string)
Checks whether the string is zero length.
|
|
String is zero length
-n (Non zero length string)
Checks whether the string is empty.
|
|
String is non zero length
Empty string check
|
|
String is not empty
Keep Learining, Keep Practicing. :) :)
Thatβs All. π
Thanks for reading. π
If you liked it feel free to share with your dear ones.π
And tell me what do you think in the comment box.π¬
Stay tuned with CS111.
This post was originally posted on Village Programmer
Author Vikash Patel
LastMod May 4, 2021