#!/bin/bash
echo "Enter two numbers"
read a b
s=`expr $a + $b`
echo "Sum1=$s"
s=$[$a+$b]
echo "sum2=$s"
((s=$a+$b))
echo "sum3=$s"
((s=a+b))
echo "sum3=$s"
let s=$a+$b
echo "sum4=$s"
let s=a+b
echo "sum4=$s"
Note:bash shell support only integer arithmetic.zsh support operations on real numbers.We can use bc in bash shell to do real arithmetic.
Eg: echo "$a*$b"|bc # where a and b are real
echo "Enter two numbers"
read a b
s=`expr $a + $b`
echo "Sum1=$s"
s=$[$a+$b]
echo "sum2=$s"
((s=$a+$b))
echo "sum3=$s"
((s=a+b))
echo "sum3=$s"
let s=$a+$b
echo "sum4=$s"
let s=a+b
echo "sum4=$s"
Note:bash shell support only integer arithmetic.zsh support operations on real numbers.We can use bc in bash shell to do real arithmetic.
Eg: echo "$a*$b"|bc # where a and b are real
Mathematical Operators With Integers
Operator | Description | Example | Evaluates To |
---|---|---|---|
+ | Addition | echo $(( 20 + 5 )) | 25 |
- | Subtraction | echo $(( 20 - 5 )) | 15 |
/ | Division | echo $(( 20 / 5 )) | 4 |
* | Multiplication | echo $(( 20 * 5 )) | 100 |
% | Modulus | echo $(( 20 % 3 )) | 2 |
++ | post-increment (add variable value by 1) | x=5 echo $(( x++ )) echo $(( x++ )) |
5 6 |
-- | post-decrement (subtract variable value by 1) | x=5 echo $(( x-- )) |
4 |
** | Exponentiation | x=2 y=3 echo $(( x ** y )) |
8 |
Comments
Post a Comment