1. 程式人生 > >[Bash]整型變數自增(加1)的幾種方法

[Bash]整型變數自增(加1)的幾種方法

#note that any space is not allowed before nor after =
#you can use or not use $ before a variable inside $(()) and $[]
a=1
a=$((a+1))
a=$((a + 1))
a=$(( a + 1 ))
a=$(( $a + 1 ))

a=$[a+1]
a=$[a + 1]
a=$[ a + 1 ]
a=$[ $a + 1 ]

#note that a space is required before and after +
a=`expr $a + 1`

let a++
let a+=1
#the result will be 12
echo $a