Bash Multiplication

In Bash scripting, you can perform arithmetic multiplication using arithmetic expansion, the expr command, and the let command. Multiplication is a basic arithmetic operation that allows you to calculate the product of two or more numbers.

In this tutorial, we’ll guide you through the different methods of performing multiplication in Bash with detailed examples and scenarios.

Bash Multiplication is one of the Arithmetic Operations in Bash.


Syntax for Multiplication in Bash

The syntax for performing multiplication in Bash can vary depending on the method used. Here are the common approaches:

Multiplication using Arithmetic Expansion:

</>
Copy
$((a * b))

Multiplication using expr:

</>
Copy
expr a \* b

Multiplication using let:

</>
Copy
let product=a*b

Multiplication using bc (for floating-point multiplication):

</>
Copy
echo "a * b" | bc

Examples of Multiplication in Bash

Let’s go through different examples to see how multiplication can be performed using these methods.


1. Bash Multiplication Using Arithmetic Expansion

Arithmetic expansion is the simplest and most common way to perform multiplication in Bash. It uses the $((...)) syntax to evaluate the expression.

example.sh

</>
Copy
#!/bin/bash

# Define two numbers
num1=5
num2=4

# Perform multiplication
product=$((num1 * num2))

# Display the result
echo "The product of $num1 and $num2 is $product."

Output

Bash Multiplication Using Arithmetic Expansion

In this example, the multiplication is performed using $((num1 * num2)), and the result is stored in the variable product.


2. Bash Multiplication Using expr

The expr command is another way to perform arithmetic operations in Bash. It is an older method but still commonly used. Note that you need to escape the * symbol with a backslash (\) to avoid shell expansion.

example.sh

</>
Copy
#!/bin/bash

# Define two numbers
num1=7
num2=3

# Perform multiplication using expr
product=$(expr $num1 \* $num2)

# Display the result
echo "The product of $num1 and $num2 using expr is $product."

Output

Bash Multiplication Using expr

In this example, we use expr to multiply num1 and num2. The \* symbol is escaped to prevent shell expansion.


3. Bash Multiplication Using let

The let command allows you to perform arithmetic operations without using $((...)). It is a simple and efficient method for integer multiplication.

example.sh

</>
Copy
#!/bin/bash

# Define two numbers
num1=6
num2=8

# Perform multiplication using let
let product=num1*num2

# Display the result
echo "The product of $num1 and $num2 using let is $product."

Output

Bash Multiplication Using let

Best Practices for Multiplication in Bash

  • Use $((...)) for simple integer multiplication as it is efficient and easy to use.
  • Escape the * symbol when using expr to prevent shell expansion.
  • Use bc for floating-point multiplication to handle decimal values accurately.
  • Validate user inputs before performing arithmetic operations to avoid errors or unexpected results.