Multiplying Bash variable is one helpful tool for data processing. The programmers often need to multiply variables for various calculations or operations to make necessary decisions from the result. Here, in this article, I will discuss some approaches to multiply bash variable. These approaches will help you to learn techniques and apply them while bash scripting.
Key Takeaways
- Learning about the let command, expr command, bc command, and arithmetic expression to multiply bash variables.
- Learning about the process of calculating multiplication and addition together.
Free Downloads
Multiply Variable in Bash
Multiplication is a simple step-by-step process in Bash. Follow the steps mentioned below to multiply two variables in Bash.
- Firstly, initialize two variables.
- After that, multiply the two variables using the * operator with $(..) or the let command, or the expr command.
- Don’t forget to assign the above multiplication to a variable.
- Print the final variable with the echo command.
Basic Syntax for Multiplication
You can calculate the multiplication of two variables using arithmetic expansion $(..), let command, or expr command. The syntax of these approaches is given below:
- Using Arithmetic Expansion
result = variable1 * variable2
- Using the let Command
let “result = variable1 * variable2”
- Using the expr Command
result=`expr $variable1 \* $variable2`
6+ Practical Examples Related to Multiply Variable in Bash
In this section, I will show some examples related to multiplying Bash variables utilizing the let command, expr command, arithmetic expansion, bc command, and for loop. After going through these examples, you will be skillful enough to do the multiplication of bash variables whenever necessary.
Example 01: Multiply Two Variables with “*” Operator and “$(())” in Bash
Here I have two variables. Now I will incorporate arithmetic expansion to calculate the multiplication of these two variables and print the result on the terminal. To know more, follow the below steps.
-
At first, launch an Ubuntu Terminal.
-
Write the following command to open a file in Nano:
nano script1.sh
EXPLANATION- nano: Opens the nano text editor.
- script1.sh: Bash script name.
-
Copy the script mentioned below:
#!/bin/bash #assigning num1 and num2 variable num1=50 num2=10 #calculating the multiplication of the two variables num1 and num2 result=$((num1 * num2)) #printing the multiplication result on the terminal echo "Multiplication of $num1 and $num2 is:$result"
EXPLANATION#! /bin/bash ‘#!’, is called shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash. After that, the num1=50 num2=10 commands have assigned a value to the num1 and the num2 variables. Subsequently, the result=$((num1 * num2)) command has calculated the multiplication of the num1 and the num2 variables and keeps the result on the result variable. Then the echo “Multiplication of $num1 and $num2 is:$result” command has printed the result on the terminal.
-
Press CTRL+O and ENTER to save the file; CTRL+X to exit.
-
Use the following command to make the file executable:
chmod +x script1.sh
EXPLANATION- chmod: Changes the permissions of files and directories.
- +x: Argument with the chmod command to add the executable permission.
- script1.sh: File that you want to make executable.
-
Run the script by the following command:
./script1.sh
The image shows the bash script has multiplicated the num1 and the num2 variables and printed the result on the terminal.
Example 02: Multiply Two Numbers Using the “expr” Command in Bash
In this example, I will use two variables & perform multiplication on them. For that, I will incorporate the expr command to calculate the multiplication of these two variables and print the result on the terminal. To know more, follow the below script.
Script (script2.sh) >
#/bin/bash
#assigning num1 and num2 variable
num1=50
num2=10
#calculating the multiplication of the two variables num1 and num2
result=`expr $num1 \* $num2`
#printing the multiplication result on the terminal
echo "Multiplication of $num1 and $num2 is:$result"
The num1=50 num2=10 commands have assigned a value to the num1 and num2 variables. Subsequently, the result=`expr $num1 \* $num2` command has calculated the multiplication of the num1 and the num2 variables and keeps the result on the result variable. Then the echo “Multiplication of $num1 and $num2 is:$result” command has printed the result on the terminal.
Run the script by executing the following command
./script2.sh
The image shows the bash script has multiplicated the num1 and the num2 variables utilizing the expr command and printed the result on the terminal.
Example 03: Bash Multiplication and Addition Using “$((..))” Expansion
Here I will develop a Bash script that will contain a for loop which will iterate from 1 to some value of the val variable and a calculation of multiplication and addition will be done for each value of the val variable utilizing the arithmetic expansion. To know more, follow the below script.
Script (script3.sh) >
#!/bin/bash
#for loop iterate the value of i from 1 to 11
for (( num =1; num <12; ++num )); do
#does the calculation for each value of i from 1 to 11
result=$(( 2*num + 1 ))
#prints each value of result
echo "$result"
done
The for (( num = 1; val <12; ++num )); do command has initiated a for loop which iterates the value of val from 0 till 11. The result=$(( 2*num + 1 )) command has multiplied each value of the val variable and added 1 utilizing the arithmetic expansion and finally the echo “$result” command has printed the result for each value of the num variable on the terminal.
Run the script by executing the following command.
./script3.sh
The image shows that the Bash script has calculated the multiplication and addition utilizing for loop and arithmetic expansion and printed the result on the terminal.
Example 04: Bash Multiplication and Addition Using the “let” Command
Here I will develop a Bash script that will contain a for loop which will iterate from 0 to some value of the val variable and a calculation of multiplication and addition will be done for each value of the val variable utilizing the let command. To know more, follow the below script.
Script (script4.sh) >
#!/bin/bash
#for loop iterate the value of val from 0 to 12
for (( val = 0; val <13; ++val )); do
#does the calculation for each value of val from 1 to 11
let answer="9*val+2"
#prints each value of the answer
echo "$answer"
done
The for (( val = 0; val <13; ++val )); do command has initiated a for loop which iterates the value of val from 0 till 12. The let answer=”9*val+2″ command has multiplied each value of the val variable and added 2 utilizing the let command and finally the echo “$answer” command has printed the result for each value of the val variable on the terminal.
Run the script by executing the following command
./script4.sh
The image shows that the Bash script has calculated the multiplication and addition utilizing for loop and let command and printed result on the terminal.
Example 05: Multiply an Assigned Variable With a Number in Bash
In this example, I have a variable named num1. I will multiply 10 with this variable num1. To know more, follow the below script.
Script (script5.sh) >
#!/bin/bash
#assigning num1 variable
num1=50
#calculating the multiplication of the variables num1 and 10
result=$((num1 * 10))
#printing the multiplication result on the terminal
echo "Multiplication of $num1 and 10 is:$result"
The num1=50 command has assigned a value to the num1 variable. Then the result=$((num1 * 10)) command has multiplied 10 with the num1 variable. Finally, the echo “Multiplication of $num1 and 10 is:$result” command has printed the result on the terminal.
Run the script by executing the following command
./script5.sh
The bash script has multiplied 10 with the num1 variable and printed the result (500) on the terminal.
Example 06: Multiply Two Float Numbers With the “bc” Command
In this example, I have two variables named float_var1 and float_var2. They contain float numbers into them. Now I will multiply the variable float_var1 and the variable float_var2 utilizing the bc command and print the result on the terminal. To know more, follow the below script.
Script (script7.sh) >
#!/bin/bash
# Define two float variables
float_var1=3.14
float_var2=2.5
# Use bc to multiply the float variables
result=$(echo "$float_var1 * $float_var2" | bc)
# Print the result
echo "The result of multiplying $float_var1 and $float_var2 is: $result"
The float_var1=3.14 float_var2=2.5 commands have defined the two variables. After that, the result=$(echo “$float_var1 * $float_var2” | bc) command has calculated the multiplication of the variable float_var1 and the variable float_var2 utilizing the bc command and kept it on the result variable. Finally, the echo “The result of multiplying $float_var1 and $float_var2 is: $result” command has printed the result on the terminal.
Run the script by executing the following command.
./script7.sh
The bash script has multiplied two float numbers utilizing the bc command and printed them on the terminal.
Bonus Example: Division of Numbers in Bash Using the “expr” Command
In this example, I have two variables named A and B. Now I will divide the variable A by variable B and print the result on the terminal. To know more, follow the below script.
Script (script6.sh) >
#!/bin/bash
#assigning A and B variable
A=25
B=5
#Division using the expr command then printing the result
echo "A / B = `expr $A / $B`"
The A=25 B=5 command has assigned value to the A and the B variables. Then the echo “A / B = `expr $A / $B`” command divides variable A by variable B with the help of the expr command and prints the result with the help of the echo command
Run the script by executing the following command.
./script6.sh
The image shows that the Bash script has divided variable A by variable B and printed the result on the terminal.
Conclusion
Here, I have tried to give you a concise idea of multiplying the bash variable. I believe after going through this article, you will be competent enough to do multiplication of bash variables wherever necessary.
People Also Ask
Related Articles
- How to Echo Variables in Bash Script? [4 Practical Examples]
- How to Use String Variables in Bash Script? [4 Cases]
- How to Append String to Bash Variable? [2 Effective Ways]
- How to Check If Bash Variable Exists? [2 Effective Methods]
- How To Check if Bash Variable is Empty? [2 Easy Methods]
- How to List and Set Bash Environment Variables? [3 Methods]
- 2 Ways to Unset Environment Variables Using Bash Script
- 5 Methods to Check If Environment Variable is Set in Bash Script
- How to Set Bash $PATH Variable? [Easiest Configuration]
- 2 Cases to Execute Command Stored in Bash Variable
- How to Store Command Output to Bash Variable? [3 Examples]
- How to Read a File into Bash Variable? [2 Simple Methods]
- How to Write Bash Variable to File? [3 Effective Methods]
- Compare Variables in Bash Scripts [3 Practical Cases]
- Increment Variable Value in Bash Scripts [4+ Examples]
- Adding 1 to Bash Variable [3 Examples]
- Decrement Variable Value in Bash Scripts [4+ Examples]
- Addition of Bash Variable [4+ Examples]
- How to Subtract Two Bash Variables? [4+ Easy Approaches]
- Variable Substitution in Bash [Replace Character & Substring]
<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial