Method 1:
Declare-i variable =$ variable 1+$ variable 2
A. Cannot have spaces between variables and =
B. There can be no spaces between variables and +
[Email protected] ~]# a=1
[Email protected] ~]# b=2
[Email protected] ~]# declare-i c= $a + $b
[Email protected] ~]# echo $c
3
Method 2:
Variable =$ (expr $ variable + $ variable)
A.= can not have space on both sides
B.+ must have spaces on both sides of the left
[Email protected] ~]# a=1
[Email protected] ~]# b=2
[[email protected] ~]# c=$ (expr $a + $b)
[Email protected] ~]# echo $c
3
Method 3:
Variable =$ ((expression))
A.= can not have space on both sides
B. Expressions are free to write freely
[Email protected] ~]# a=1
[Email protected] ~]# b=2
[[email protected] ~]# c=$ (($a + $b))
[Email protected] ~]# echo $c
3
Method 3:
Variable =$[-op]
A.= can not have space on both sides
B. Expressions are free to write freely
[Email protected] ~]# a=1
[Email protected] ~]# b=2
[[email protected] ~]# c=$[$a + $b + 1]
[Email protected] ~]# echo $c
4
Numerical operations in Linux