Operators in Bash Shell Programming

WORDS: 1126 | CODE BLOCKS: 25 | EXT. LINKS: 3

A Picture of Pluto by NASA featured on Village Programmer 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:

bash
1$x=20
2$y=30
3a=`expr $x + $y`
4echo $a

This will display:

50

NOTE:
  1. The expr command evaluates the expression.
  2. 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:

bash
1$x=40
2$y=30
3a=`expr $x - $y`
4echo $a

This will display:

10

1.3 Multiplication Operator

It’s a * that multplies operands together.
Shown in the below example:

bash
1$x=2
2$y=3
3a=`expr $x * $y`
4echo $a

This will display:

6

1.4 Division Operator

It’s a / that performs division operation.
Shown in the below example:
-Example 1-

bash
1$x=200
2$y=50
3a=`expr $x / $y`
4echo $a

This will display:

4

-Example 2-

bash
1$x=20
2$y=3
3a=`expr $x / $y`
4echo $a

This will display:

6

1.5 Modulus Operator

It’s a % that performs modulus operation and returns the remainder.
Shown in the below example:

bash
1$x=10
2$y=3
3a=`expr $x % $y`
4echo $a

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-

bash
1$x=10
2$y=10
3a=`expr $x == $y`
4echo $a

This will display:

1

-Example 2-

bash
1$x=10
2$y=5
3a=`expr $x == $y`
4echo $a

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-

bash
1$x=10
2$y=10
3a=`expr $x == $y`
4echo $a

This will display:

0

-Example 2-

bash
1$x=10
2$y=5
3a=`expr $x == $y`
4echo $a

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.

bash
1a=10
2b=10
3if [ $a -eq $b ]
4	then
5	echo "Equal"
6else
7	echo "Not Equal"
8fi

Equal

-ne (Not equals to operator)

This checks whether the operands are equal.
This operator is used to perform comparison in the if condition.

bash
1a=10
2b=20
3if [ $a -ne $b ]
4	then
5	echo "Not Equal"
6else
7	echo "Equal"
8fi

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.

bash
1a=40
2b=20
3if [ $a -gt $b ]
4	then
5	echo "$a is greater than $b"
6else
7	echo "$a is not greater than $b"
8fi

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.

bash
1a=20
2b=40
3if [ $a -lt $b ]
4	then
5	echo "$a is less than $b"
6else
7	echo "$a is not less than $b"
8fi

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.

bash
1a=40
2b=20
3if [ $a -ge $b ]
4	then
5	echo "$a is greater than or equals to $b"
6else
7	echo "$a is not greater than $b"
8fi

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.

bash
1a=40
2b=200
3if [ $a -le $b ]
4	then
5	echo "$a is less than or equals to $b"
6else
7	echo "$a is not less than $b"
8fi

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-

bash
1$x=10
2$y=10
3a=`expr $x == $y`
4echo $a

This will display:

0

-Example 2-

bash
1$x=10
2$y=5
3a=`expr $x == $y`
4echo $a

This will display:

1

-o (OR operator)

Checks whether one of the operands is true and executes the statements.

bash
1a=40
2b=20
3c=10
4if [ $a -gt $b -o $c -gt $b]
5	then
6	echo "$a or $c is greater than $b"
7else
8	echo "$a is not less than $b"
9fi

40 or 10 is greater than 20

-a (AND operator)

Checks whether all of the operands are true and executes the statements.

bash
1a=40
2b=20
3c=100
4if [ $a -gt $b -a $c -gt $b]
5	then
6	echo "$a and $c are greater than $b"
7else
8	echo "$a is not less than $b"
9fi

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.

bash
1user="VIKAS"
2defaultUser="VIKAS"
3if [ $user = $defaultUser ]
4then
5	echo "User is allowed"
6else
7	echo "User is not allowed"
8fi

User is allowed

!= (Not equal operator)

Check whether two strings are not equal.

bash
1user="VIKAS"
2defaultUser="DAVE"
3if [ $user != $defaultUser ]
4then
5	echo "User is not allowed"
6else
7	echo "User is allowed"
8fi

User is not allowed

-z (Zero length string)

Checks whether the string is zero length.

bash
1s=""
2if [ -z $s ]
3then
4	echo "String is zero length"
5else
6	echo "String is non zero length"
7fi

String is zero length

-n (Non zero length string)

Checks whether the string is empty.

bash
1s="Village Programmer"
2if [ -n $s ]
3then
4	echo "String is non zero length"
5else
6	echo "String is zero length"
7fi

String is non zero length

Empty string check

bash
1s=""
2if [ $s ]
3then
4	echo "String is not empty"
5else
6	echo "String is empty"
7fi

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