In this part,I will show you how to do some arithmetic calculation in Bash.
1.COMMAND:
expr
DESCRIPTION:
Print the value of EXPRESSION to standard output.
SYNOPSIS:
expr EXPRESSION
Example:
root@piniheaven:~# expr 1 + 1
2
root@piniheaven:~# expr 2 - 1
1
root@piniheaven:~# expr 4 / 2
2
root@piniheaven:~# expr 2 \* 2 #Tip: when you want to do some multiplication,you should use backslash
4
2.COMMAND:
let
DESCRIPTION:
carry out arithmetic operations on variables.
Example:
root@piniheaven:~# let " val=(1+1)*2" # will compute 2 times 2 and assign the result to the variable "val".
root@piniheaven:~# echo $val #print variable val
4
1.COMMAND:
expr
DESCRIPTION:
Print the value of EXPRESSION to standard output.
SYNOPSIS:
expr EXPRESSION
Example:
root@piniheaven:~# expr 1 + 1
2
root@piniheaven:~# expr 2 - 1
1
root@piniheaven:~# expr 4 / 2
2
root@piniheaven:~# expr 2 \* 2 #Tip: when you want to do some multiplication,you should use backslash
4
2.COMMAND:
let
DESCRIPTION:
carry out arithmetic operations on variables.
Example:
root@piniheaven:~# let " val=(1+1)*2" # will compute 2 times 2 and assign the result to the variable "val".
root@piniheaven:~# echo $val #print variable val
4