Arithmetic expansion

$(( <EXPRESSION> ))

$[ <EXPRESSION> ]

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"
else
  echo "Haha... :-P"
fi

See also

syntax/expansion/arith.txt · Last modified: 2009/02/24 07:26 (external edit)
www.chimeric.de Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0