Bash

Bash Arithmetic

Syntax#

  • $(( EXPRESSION )) - Evaluates expression and returns its result.

  • expr EXPRESSION - Prints result of EXPRESSION to stdout.

Parameters#

Parameter Details
EXPRESSION Expression to evaluate
## Remarks#
A space (” ”) is required between each term (or sign) of the expression. “1+2” won’t work, but “1 + 2” will work.
## Arithmetic command

Simple arithmetic with (( ))

#!/bin/bash
echo $(( 1 + 2 ))

Output: 3

# Using variables
#!/bin/bash
var1=4
var2=5
((output=$var1 * $var2))
printf "%d\n" "$output"

Output: 20

Simple arithmetic with expr

#!/bin/bash
expr 1 + 2

Output: 3


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow