How to Multiply Variables in Bash
-
Method 1: Using the
expr
Command - Method 2: Using Arithmetic Expansion
-
Method 3: Using the
bc
Command for Floating Point Arithmetic -
Method 4: Using
let
Command - Conclusion
- FAQ

In the world of scripting, Bash is a powerful tool for automating tasks and managing system operations. One common requirement is performing arithmetic operations, such as multiplying variables. Whether you’re a seasoned developer or a beginner looking to enhance your scripting skills, understanding how to multiply variables in Bash is essential.
This tutorial will walk you through various methods to achieve multiplication in Bash, complete with practical examples and clear explanations. By the end, you’ll be equipped with the knowledge to confidently handle multiplication operations in your Bash scripts.
Method 1: Using the expr
Command
One of the simplest ways to multiply variables in Bash is by using the expr
command. This command evaluates expressions and performs arithmetic operations. To multiply two variables, you simply need to pass the variables to expr
with the multiplication operator *
. However, remember that in Bash, the *
symbol needs to be escaped or enclosed in quotes to avoid being treated as a wildcard.
Here’s how you can do it:
#!/bin/bash
a=5
b=10
result=$(expr $a \* $b)
echo "The result of multiplying $a and $b is: $result"
Output:
The result of multiplying 5 and 10 is: 50
In this example, we first assign values to the variables a
and b
. We then use expr
to perform the multiplication, storing the result in the variable result
. Finally, we print the result to the console. This method is straightforward and effective for simple arithmetic tasks.
Method 2: Using Arithmetic Expansion
Another efficient way to multiply variables in Bash is through arithmetic expansion. This method allows you to perform arithmetic calculations directly within the script using the $((...))
syntax. It’s cleaner and avoids some of the pitfalls of the expr
command.
Here’s how you can implement this:
#!/bin/bash
a=5
b=10
result=$((a * b))
echo "The result of multiplying $a and $b is: $result"
Output:
The result of multiplying 5 and 10 is: 50
In this code snippet, we again define our variables a
and b
. The multiplication is performed using the $((...))
syntax, which evaluates the expression inside the parentheses. This method is not only more concise but also easier to read, making it a popular choice among Bash scripters.
Method 3: Using the bc
Command for Floating Point Arithmetic
If you need to perform multiplication that involves floating-point numbers, the previous methods may not suffice, as Bash primarily deals with integers. In such cases, the bc
command is your best friend. bc
is a calculator utility that can handle decimal numbers and perform complex calculations.
Here’s a quick example:
#!/bin/bash
a=5.5
b=10.2
result=$(echo "$a * $b" | bc)
echo "The result of multiplying $a and $b is: $result"
Output:
The result of multiplying 5.5 and 10.2 is: 56.10
In this example, we define two floating-point variables, a
and b
. We then use echo
to pass the multiplication expression to bc
, which calculates the result. This method is particularly useful when accuracy with decimal numbers is required in your calculations.
Method 4: Using let
Command
The let
command is another way to perform arithmetic operations in Bash. It allows you to evaluate an arithmetic expression and assign the result to a variable. This method is quite straightforward and can be used for both integer and floating-point operations, though it is typically used for integers.
Here’s how it works:
#!/bin/bash
a=5
b=10
let result=a*b
echo "The result of multiplying $a and $b is: $result"
Output:
The result of multiplying 5 and 10 is: 50
In this script, we use let
to evaluate the multiplication of a
and b
. The result is stored in the variable result
, which we then print. While let
is a less commonly used method, it is still effective for quick calculations.
Conclusion
Multiplying variables in Bash is a fundamental skill that can greatly enhance your scripting capabilities. Whether you choose to use expr
, arithmetic expansion, bc
, or let
, each method has its advantages depending on your specific needs. By understanding these techniques, you can perform arithmetic operations seamlessly within your Bash scripts, making your automation tasks more efficient. So, go ahead and experiment with these methods in your next project!
FAQ
-
How do I multiply two integers in Bash?
You can multiply two integers in Bash using theexpr
command or arithmetic expansion with$((...))
. -
Can I multiply floating-point numbers in Bash?
Yes, you can multiply floating-point numbers in Bash using thebc
command for accurate calculations. -
What is the difference between
let
andexpr
in Bash?
let
is used for arithmetic operations without needing to escape operators, whileexpr
requires escaping the multiplication operator. -
Is there a way to perform multiple arithmetic operations in one line?
Yes, you can use arithmetic expansion$((...))
to perform multiple operations in a single expression. -
What should I do if my multiplication results in a decimal?
Use thebc
command, which can handle floating-point arithmetic and provide accurate results.
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook