How to Multiply Variable in Bash [6+ Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

To multiply variable in Bash, you can follow these steps:

  1. Firstly, initialize two variables.
  2. After that, multiply the two variables using the * operator with $(..) or the let command, or the expr command.
  3. Don’t forget to assign the above multiplication to a variable.
  4. 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:

  1. Using Arithmetic Expansion
    result = variable1 * variable2
  2. Using the let Command
    let “result = variable1 * variable2”
  3. 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"

EXPLANATION

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 bash script has multiplid the num1 and the num2 bash variables and 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"

EXPLANATION

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 bash script has multiplicated the num1 and the num2 variables utilizing the expr command and 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

EXPLANATION

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 Bash script has calculated the multiplication and addition utilizing for loop and arithmetic expansion and printed the result 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

EXPLANATION

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 Bash script has calculated the multiplication and addition utilizing for loop and let command and printed result 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"
EXPLANATION

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.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"
EXPLANATION

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.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`"
EXPLANATION

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 Bash script has divided variable A by variable B and printed the result on the terminal.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


<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial

4.8/5 - (6 votes)
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment