Arithmetic operation using double parentheses construct in Bash
October 18, 2008
By: Neo
In Category Bash
The following script illustrates how double parentheses can be used to do arithmetic operations.
#!/bin/sh (( a = 23 )) echo "a (assign value) = $a" (( ++a )) echo "a (after ++a) = $a" (( --a )) echo "a (after --a) = $a" (( a++ )) echo "a (after a++) = $a" (( a-- )) echo "a (after a--) = $a" echo (( b = a>30?12:20 )) echo "If a > 30, then b = 12, else b = 20." echo "b = $b " echo
The following is output of the above script.
[neo@techpulp ~]# sh dpara.sh a (assign value) = 23 a (after ++a) = 24 a (after --a) = 23 a (after a++) = 24 a (after a--) = 23 If a > 30, then b = 12, else b = 20. b = 20 [neo@techpulp ~]#
Here is an another example to use at shell prompt.
[neo@techpulp ~]# echo $((24/12)) 2 [neo@techpulp ~]#