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"

This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information
Jochen, 2012/07/17 07: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 07:05

Fixed, thx

Yclept Nemo, 2012/11/27 01:51, 2012/11/30 19:40

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

You could leave a comment if you were logged in.
  • syntax/expansion/arith.1235456762.txt
  • Last modified: 2011/10/05 05:17
  • (external edit)