Bash Multiplication and Addition

The bash script allows arithmetic operations, i.e., addition, subtraction, multiplication, and many others similar to other programming languages. These operations use the bash arithmetic operators to perform this task.

The way of performing basic arithmetic operations like multiplication and addition in bash differs from other languages. However, new users of Linux may need clarification about adding and multiplying numbers in the bash scripts.

This guide explains bash multiplication and addition in Linux:

Bash Multiplication and Addition Using “$((..))” Expansion

The $((..)) is known as the “arithmetic expansion operator” that translates the string into the numerical expression. It supersedes the use of backticks and double quotes in bash scripts. Let’s show how it works in a bash script for multiplication and addition arithmetic operations.

Create/Open a Script

A “code.sh” script is created using the “nano” text editor having the following lines of code:

$ nano code.sh

Code: 

#!/bin/sh

for (( num =1; num <12; ++num )); do
  ans=$(( 2*num + 1 ))
  echo "$ans"
done

The description of the above code block is here:

  • The “#!/bin/bash” represents the “Bash Shebang” that executes the script in the bash shell.
  • The “for” loop initialization statement contains a “num” variable, i.e., “num=1”. The next “num<12” specifies the “test expression,” i.e., the loop will iterate till the “num is less than 12”. The last “++num” shows an update statement that increments the value of “$ans” after every iteration. 
  • The “ans” variable enclosed in “$((..))” performs the multiplication operation, i.e., “2*num,” and then adds “1” in the multiplied value.
  • The “echo” statement prints the “$ans” variable value in the terminal.
  • The “do” and “done” refers to the starting and ending parameters of the “for” loop.

Press “Ctrl+S” to save the code and “Ctrl+X” to exit the editor from the terminal.

Make the Script Executable

In case the script is newly created, then make it executable by assigning the “x(execute)” permissions via the “chmod” command:

$ sudo chmod +x code.sh

The “code.sh” script is now executable.

Output:

Execute the “code.sh” script and see the output:

$ ./code.sh

The “code.sh” script has been executed successfully, showing that the “for” loop is iterated “11” times.

Bash Multiplication and Addition Using “expr” Command

The “expr” command line utility translates the defined parameters and returns the appropriate output.

It allows arithmetic, as well as string operations in bash. This section explains using “expr” for multiplication and addition in the bash script.

Create/Open a Script

The “program.sh” existing script is opened in the “nano” text editor:

$ nano program.sh

Code:

#!/bin/sh

for (( var = 0; var <=10; ++var )); do
  result=$( expr 5 '*' "var" + 2 )
  echo "$var"
done

The “program.sh” code is illustrated below:

  • The “for” loop starts from the “0” value of “var” variable and will iterate until the condition “var is less than and equal to 10”, i.e., “var<=10” becomes fall. The “++var” denotes the incremental statement.
  • The “result” variable uses the “expr” command that performs the multiplication operation first, i.e., “5*$var,” and then adds the integer “2” in it.
  • The “echo” statement displays the “$var” output.

Save and close the script.

Output:

The execution of the “program.sh” script is as follows:

$ ./program.sh

The output contains 11 values starting from 0 and ending at “10”.

Bash Multiplication and Addition Using the “let” Command

The “let” command corresponds to the simplest way of representing the arithmetic expression in bash script. It reads the string and translates it into its stored integer value. Let’s see its practical implementation.

Create/Open a Script

The bash script named “sample.sh” is taken as an example that contains the below-mentioned content:

$ nano sample.sh

Code:    

#!/bin/bash
for (( val = 0; val <13; ++val )); do
let answer="9*val+2"
  echo "$answer"
done

The code is explained here:

  • The “for” loop contains a “val” variable that initializes from “0” and checks the test condition until its value is less than 13. The updated statement is “++val”.
  • The “let” command declares the “answer” variable having the arithmetic expression “9*val+2”. 
  • The “echo” statement shows the output of the “$answer” variable.

Close the “nano” editor.

Output:

Run the “sample.sh” script to check its output in the terminal:

$ ./sample.sh

The list of “13” integer values has been displayed in the terminal.

Conclusion

In Linux, the bash addition and multiplication arithmetic operations can be performed using the “arithmetic expansion”, and the “expr” command line tool. This task can also be performed using the “let” command line utility. These tools convert the argument strings into integer values and return the output.

This post has covered all possible ways to perform bash addition and multiplication in Linux.