How to Use Expressions $(()), (()) in Bash?

In the bash script, the expressions “(()),” and “$(())” are utilized for the calculation of the various expressions containing more than one argument. The only difference between “(())” and “$(())” is, the “(())” doesn’t return the output while the “$(())” returns the results and stores it in the defined variable. 

This post will briefly demonstrate the usage of expressions “$(())” and “(())” in the bash script.

  • Expression “$(())” and “(())” in Bash
    • Perform Arithmetic Operations Using “$(())”
    • Check whether the Number is Even or Odd Using “(())” 
    • Expression “$(())” and “(())” in a Loop 

Expression “$(())” and “(())” in Bash

If the user uses the “(())” expression in the echo statement, it will print nothing while “$(())” prints the result value. To make it clear, the subsequent examples are performed are as below:

Example 1: Perform Arithmetic Operations Using “$(())” Expression

An example is considered to perform the arithmetic operations such as addition, subtraction, multiplication, and division using the “$(())” expression in the following script:

#!/bin/bash

a=4;

b=2;

echo "The Addition is $((a+b))"

echo "The Subtraction is $((a-b))"

echo "The Multiplication is $((a*b))"

echo "The Division is $((a/b))"

The description of the script is given below:

  • Firstly, two variables “a” and “b” are initialized with values 4 and 2.
  • After that, the echo statements are performed in addition to the variable “a” and “b” using “$((a+b))” and printing it on the screen. 
  • This way, subtraction, multiplication, and division operations are performed and printed on the screen. 

Save the script file by pressing “Ctrl+O” and exit from the file by pressing “Ctrl+X” in the text editor. 

Run the script file (script.sh) with the “bash” keyword to obtain the results in the terminal:

$ bash script.sh

The addition of two variables “a” and “b” is 6, subtraction is 2, multiplication is 8, and division is “2”.

Example 2: Check if the Number is Even or Odd Using “(())” Expression

As the “(())” expression doesn’t return the calculation results but can be used as a conditional statement to perform other tasks. The following script shows the usage of the “(())” expression in which even and odd numbers are calculated:

#!/bin/bash

read -p "Enter Number to Check: " num

((num%2==0)) && echo "The Number is Even"

The script is defined as below: 

  • The “read” property with a “p” flag is used to get the numbers from the user and store it in the “num” variable.
  • The “((num%2==0))” expression is utilized to check if the number is even or odd. After that, the “echo” command is separated by “&&” to print the message in the terminal.

Save the script file and exit.

Run the “script.sh” file in the terminal:

$ bash script.sh

The entered number 6 is even, and it displays the “The Number is Even” in the terminal.

Example 3: Print Numbers Using “$(())” and “(())” in a Loop 

The following code prints the first numbers from 1 to 10 using the “$(())” and “(())” expressions:

#!/bin/bash

a=1
while ((a<=10)) #(()) Expression to Check the Condition
do
  echo "$a"

a=$((a+1)) #$(()) Expression incrementing and storing in variable a.

done

The description of the script is mentioned below: 

  • The “a” variable is initialized with 1.
  • The while loop with the expression “((a<=10))” is utilized to check the number is less than and equal to 10.
  • After that, the “do” statement prints the “a” variable.
  • The “a=$((a+1))” expression is utilized to increment the variable “a” to continue the loop.

Save the file and exit the text editor.

Run the script file to retrieve the results:

$ bash script.sh

The output shows that the numbers from 1 to 10 have been printed on the screen.

Conclusion 

To perform calculations in bash the expression “(())” and “$(())” are used, the “$(())” returns the results while “(())” doesn’t and is applied on conditions. The “$(())” is considered for storing the results in a variable or printing it on the screen. On the other side, the “(())” expression is used to check the conditional statement. 

This write-up has illustrated the usage of expressions “$(())” and “(())” in the bash script.