Bash Operators

  • Arthmetic Operatos        --------------------------> +,-,*,/ and %
  • Numeric comparision operators    ----------------> -gt,-ge,-lt,-le,-eq,-ne
  • String comparision operators       --------------------  <,>,= and !=
  • Logical Operators:        -----------------  a (and),-o(or),!(not)

------------------------------------------------------------------------------------------------------------------------------------
Example:1
------------------------------------------------------------------------------------------------------------------------------------
Bash Script

[haritaraka12078@cxln4 ~]$ cat>arth.sh
echo "enter two numbers:"
read a b
c=`expr $a + $b`
echo "the sum of two Numbers: $c"

c=`expr $a - $b`
echo "the sub of two Numbers: $c"

c=`expr $a \* $b`
echo "the mul of two Numbers: $c"

c=`expr $a / $b`
echo "the div of two Numbers: $c"


-------------
[haritaraka12078@cxln4 ~]$ ./arth.sh
enter two numbers:
100 200
the sum of two Numbers: 300
the sub of two Numbers: -100
the mul of two Numbers: 20000
the div of two Numbers: 0


------------------------------------------------------------------------------------------------------------------------------------
Example:  Double Parentheses
------------------------------------------------------------------------------------------------------------------------------------

[haritaraka12078@cxln4 ~]$ sum=$((10+3))
[haritaraka12078@cxln4 ~]$ echo "sum=$sum"
sum=13


[haritaraka12078@cxln4 ~]$ ((Sum=10+3))
[haritaraka12078@cxln4 ~]$ echo "Sum = $Sum"
Sum = 13


[haritaraka12078@cxln4 ~]$ Num1=10
[haritaraka12078@cxln4 ~]$ Num2=3
[haritaraka12078@cxln4 ~]$ ((Sum=Num1+Num2))
[haritaraka12078@cxln4 ~]$ echo "Sum = $Sum"
Sum = 13


[haritaraka12078@cxln4 ~]$ Num1=10
[haritaraka12078@cxln4 ~]$ Num2=3
[haritaraka12078@cxln4 ~]$ Sum=$((Num1+Num2))
[haritaraka12078@cxln4 ~]$ echo "Sum = $Sum"
Sum = 13



------------------------------------------------------------------------------------------------------------------------------------
Example:  
------------------------------------------------------------------------------------------------------------------------------------
Bash Script

[haritaraka12078@cxln4 ~]$ cat>oper.sh
#!/bin/bash

x=8
y=2
echo "x=8, y=2"
echo "Addition of x & y"
echo $(( $x + $y ))
echo "Subtraction of x & y"
echo $(( $x - $y ))
echo "Multiplication of x & y"
echo $(( $x * $y ))
echo "Division of x by y"
echo $(( $x / $y ))
echo "Exponentiation of x,y"
echo $(( $x ** $y ))
echo "Modular Division of x,y"
echo $(( $x % $y ))
echo "Incrementing x by 5, then x= "
(( x += 5 ))
echo $x
echo "Decrementing x by 5, then x= "
(( x -= 5 ))
echo $x
echo "Multiply of x by 5, then x="
(( x *= 5 ))
echo $x
echo "Dividing x by 5, x= "
(( x /= 5 ))
echo $x
echo "Remainder of Dividing x by 5, x="
(( x %= 5 ))
echo $x


-------------
[haritaraka12078@cxln4 ~]$ ./oper.sh
x=8, y=2
Addition of x & y
10
Subtraction of x & y
6
Multiplication of x & y
16
Division of x by y
4
Exponentiation of x,y
64
Modular Division of x,y
0
Incrementing x by 5, then x=
13
Decrementing x by 5, then x=
8
Multiply of x by 5, then x=
40
Dividing x by 5, x=
8
Remainder of Dividing x by 5, x=
3


------------------------------------------------------------------------------------------------------------------------------------
Example:  LET Keyword : 
Let is a built-in command of Bash that allows us to perform arithmetic operations
------------------------------------------------------------------------------------------------------------------------------------
Bash Script

[haritaraka12078@cxln4 ~]$ cat>letoper.sh
#!/bin/bash

x=10
y=6
z=0
echo "Addition"
let "z = $(( x + y ))"
echo "z= $z"

echo "Substraction"
let "z = $((x - y ))"
echo "z= $z"

echo "Multiplication"
let "z = $(( x * y ))"
echo "z = $z"

echo "Division"
let "z = $(( x / y ))"
echo "z = $z"

echo "Exponentiation"
let "z = $(( x ** y ))"
echo "z = $z"

echo "Modular Division"
let "z = $(( x % y ))"
echo "z = $z"

let "x += 5"
echo "Incrementing x by 5, then x= "
echo $x

let "x -= 5"
echo "Decrementing x by 5, then x= "
echo $x

let "x *=5"
echo "Multiply of x by 5, then x="
echo $x

let "x /= 5"
echo "Dividing x by 5, x= "
echo $x

let "x %= 5"
echo "Remainder of Dividing x by 5, x="
echo $x


-------------
[haritaraka12078@cxln4 ~]$ ./letoper.sh
Addition
z= 16
Substraction
z= 4
Multiplication
z = 60
Division
z = 1
Exponentiation
z = 1000000
Modular Division
z = 4
Incrementing x by 5, then x=
15
Decrementing x by 5, then x=
10
Multiply of x by 5, then x=
50
Dividing x by 5, x=
10
Remainder of Dividing x by 5, x=
0



------------------------------------------------------------------------------------------------------------------------------------
Example:  Backtrick
can also be performed using backticks and expr (known as all-purpose expression evaluator). The `expr` is similar to 'let,' but it does not save the result to a variable.
------------------------------------------------------------------------------------------------------------------------------------

Bash Script

[haritaraka12078@cxln4 ~]$ cat>backtrick.sh
#!/bin/bash
#Basic arithmetic using expr

echo "a=10, b=3"
echo "c is the value of addition c=a+b"
a=10
b=3
echo "c= `expr $a + $b`"


-------------

[haritaraka12078@cxln4 ~]$ ./backtrick.sh
a=10, b=3
c is the value of addition c=a+b
c= 13