Simple math

The double parenthesis construct in bash allows for arithmetic expansion. Using this in the simplest format, we can easily carry out integer arithmetic. This becomes a replacement for the let built-in. The following examples show the use of the let command and the double parenthesis to achieve the same result:

$ a=(( 2 + 3 ))$ let a=2+3

In both cases, the a parameter is populated with the sum of 2 + 3. If you want to write it on a shell script, you need to add a dollar sign before the parentheses:

#!/bin/bashecho $(( 2 + 3 ))

Get Mastering Linux Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.