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

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.

  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.

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:

  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

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.

Steps to Follow >

  1. At first, launch an Ubuntu Terminal.

  2. 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.
  3. 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.

  4. Press CTRL+O and ENTER to save the file; CTRL+X to exit.

  5. 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.
  6. Run the script by the following command:

    ./script1.sh

    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.

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.

You can follow the steps mentioned in Example 01 to know how to write, save, and make the bash script executable.

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"

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.

Run the script by executing the following command

./script2.sh

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.

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.

You can follow the steps mentioned in Example 01 to know how to write, save, and make the bash script executable.

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

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.

Run the script by executing the following command.

./script3.sh

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.

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.

You can follow the steps mentioned in Example 01 to know how to write, save, and make the bash script executable.

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

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.

Run the script by executing the following command

./script4.sh

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.

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.

You can follow the steps mentioned in Example 01 to know how to write, save, and make the bash script executable.

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

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

You can follow the steps mentioned in Example 01 to know how to write, save, and make the bash script executable.

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

You can follow the steps mentioned in Example 01 to know how to write, save, and make the bash script executable.

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

Run the script by executing the following command.

./script6.sh

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 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

Can you multiply a variable with a constant?
You can easily multiply a variable with a constant using the following syntax.

result=$((num1 * [value of constant]))

How do you combine variables in bash?
For concatenating variables you can follow this syntax.

X=”String”

Y=”Concatenation!”

echo “${X}${Y}”

These commands 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 you multiply random variables?

You can multiply random variables by following proper syntax. Some syntaxs are given below.

Using Arithmetic Expansion

result =  variable1 * variable2

Using the let command

let “result = variable1 * variable2”

Using the expr command

result=`expr $variable1 \* $variable2`

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