This is an old revision of the document!

Arithmetic expansion



The arithmetic expression <EXPRESSION> is evaluated and the resulting value replaces the whole $((...)) construct:

Please do not use the second form $[ … ]! It's deprecated. The preferred and standardized form is $(( ... ))!

read -p "Enter first operand: " first
read -p "Enter second operand: " second
echo "The sum is $((first + second))"

Note that in Bash you don't need the arithmetic expansion to simply check for a specific value of an integer, this can be done using the arithmetic evaluation compound command:

read -p "Enter a number: "
if ((number == 1234)); then
  echo "Good guess"
  echo "Haha... :-P"

Jochen, 2012/07/17 09:59

The line

read -p "Enter a number: "

in the second example should read

read -p "Enter a number: " number

Jan Schampera, 2012/08/12 09:05

Fixed, thx

Yclept Nemo, 2012/11/27 02:51, 2012/11/30 20:40

Should mention that $(()) form doesn't accept quoted variable names.

You could leave a comment if you were logged in.