FUNDAMENTALS A Complete Guide for Beginners
To multiply variable in Bash, you can follow these steps:
- 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.
In this article, I will discuss the steps to multiply the bash variable in detail. Keep reading.
Basic Syntax to Multiply Bash Variable
You can multiply 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
Multiplying Bash variable is one helpful tool for data processing. 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 multiply bash variables whenever necessary.
1. Multiply Two Variables with “*” Operator and “$(())” in Bash
To multiply the variable in bash, you can use the “*” operator and “$(())”. 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. Check out the bash script:
#!/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"
Here, the num1=50 and 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.
The image shows the bash script has multiplicated the num1 and the num2 variables and printed the result on the terminal.
2. Multiply Two Variables Using the “expr” Command in Bash
To multiply a variable in bash. you can use the expr
command. 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:
#/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.
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.
3. Multiply Bash Variables Using “$((..))” Expansion
Here to multiply bash variables, I will develop a Bash script that will contain a for loop
which will iterate from 1 to 11 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 accomplish this task, check the below script:
#!/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.
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.
4. Multiply Variable in Bash 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. Follow the below script:
#!/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.
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.
5. Multiply an Assigned Variable With a Number in Bash
To multiply an assigned variable with a number, check this example. Here, I have a variable named num1. I will multiply 10 with this variable num1. To accomplish this, write the below script:
#!/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.
The bash script has multiplied 10 with the num1 variable and printed the result (500) on the terminal.
6. Multiply Two Float Numbers With the “bc” Command
To multiply float numbers in bash, you can use the bc
command. In this example, I have two float 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. Here’s the bash script:
#!/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 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.
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
To perform division operations in bash, check this example thoroughly. 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. Here’s the bash script for this:
#!/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.
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 how to multiply 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
How to multiply two variables in Bash?
To multiply two variables number1 and number2 in Bash, you can use this syntax: multiplication=$((number1 * number2))
.
Can I multiply a variable with a constant?
Yes, you can easily multiply a variable with a constant using the following syntax:
result=$((num1 * [value of constant]))
How to combine variables in bash?
To combine variables in bash, you can follow this script:
X="String"
Y="Concatenation!"
echo "${X}${Y}"
This script will print “String Concatenation!” On the terminal.
What represents multiplication?
Multiplication is represented by an asterisk (*) and is used in computer languages. Cross(x) is used for writing on the notebook.
Can I multiply random variables?
Yes, you can multiply random variables by using arithmetic expansion, let, and expr commands.
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