+→ Addition-→ Subtraction*→ Multiplication (use\*as*is a wildcard character)/→ Division%→ Modulo Operator
-gt→ Greater than-ge→ Greater than or equal to-lt→ Less than-le→ Less than or equal to-eq→ Equal to-ne→ Not equal to
These operators return boolean values (
true/false).
-a→ Logical AND-o→ Logical OR!→ Logical NOT
=
Note:
- Except for the assignment operator, spaces are mandatory before and after other operators.
There are multiple ways to perform mathematical operations:
expr stands for expression.
#!/bin/bash
read -p "Enter First Number: " a
read -p "Enter Second Number: " b
sum=`expr $a + $b`
echo "The Sum: $sum"
sum=$(expr $a + $b)
echo "The Sum: $sum"Notes:
$symbol is mandatory.- Space is required before and after the
+symbol.
let sum=a+b
echo "The Sum: $sum"
let sum=$a+$b
echo "The Sum: $sum"Notes:
$symbol is optional.- Do not use spaces before and after the
+.
sum=$((a+b))
echo "The Sum: $sum"
sum=$(($a+$b))
echo "The Sum: $sum"Notes:
- Space and
$symbol are optional.
sum=$[a+b]
echo "The Sum: $sum"
sum=$[$a+$b]
echo "The Sum: $sum"Notes:
- Space and
$symbol are optional.
bc (Binary Calculator) is used to perform both integer and floating-point arithmetic. Start bc by typing bc in the terminal:
durgasoft@durgasoft:~/scripts$ bc
bc 1.07.1
10.5+30.6
41.1
10.2^2
104.0
10.5*3.6
37.8
ctrl+d # Exit bc#!/bin/bash
read -p "Enter First Number: " a
read -p "Enter Second Number: " b
sum=$(echo $a+$b | bc)
echo "The Sum: $sum"
echo "The Difference: $(echo $a-$b | bc)"
echo "The Product: $(echo $a*$b | bc)"Output:
durgasoft@durgasoft:~/scripts$ ./test.sh
Enter First Number: 10.5
Enter Second Number: 5.3
The Sum: 15.8
The Difference: 5.2
The Product: 55.6#!/bin/bash
read -p "Enter First Number: " a
read -p "Enter Second Number: " b
sum=$[a+b]
echo "The Sum: $sum"#!/bin/bash
read -p "Enter Any 4-Digit Integer Number: " n
a=$(echo $n | cut -c 1)
b=$(echo $n | cut -c 2)
c=$(echo $n | cut -c 3)
d=$(echo $n | cut -c 4)
echo "The Sum of 4 Digits: $[a+b+c+d]"Output:
durgasoft@durgasoft:~/scripts$ ./test.sh
Enter Any 4-Digit Integer Number: 1234
The Sum of 4 Digits: 10
durgasoft@durgasoft:~/scripts$ ./test.sh
Enter Any 4-Digit Integer Number: 3456
The Sum of 4 Digits: 18#!/bin/bash
read -p "Enter Employee Monthly Salary: " salary
annual_salary=$[salary*12]
bonus=$[annual_salary*25/100]
echo "The Bonus: $bonus"Output:
durgasoft@durgasoft:~/scripts$ ./test.sh
Enter Employee Monthly Salary: 10000
The Bonus: 30000