Addition is one of the most basic arithmetic operations. In data processing, programmers need to add variables to make calculations and make subsequent decisions. In this article, I will explore different approaches to the addition of the Bash variable. It includes arithmetic expansion, expr command, bc command, and last but not least awk command approach.
Key Takeaways
- Learning about the process of variable addition for necessary data manipulation.
- Knowing about the process of incorporating the expr command, the awk command, and the bc command to calculate the summation of the bash variable.
Free Downloads
Add Two Variables in the Bash Scripts
Using some basic expressions or commands you can easily perform variable addition in bash scripts. Here, some common approach to adding two variable in Bash is given below:
1. Straightforward approach to add two variables in bash.
sum=$(($num1 + $num2))
2. Using expr command with quotes
sum='expr $num1 + $num2'
3. Use the expr command enclosed with brackets and start with the dollar symbol.
sum=$(expr $num1 + $num2)
4. Using bc command
echo $num1 + $num2 | bc -l
5. Using awk command
echo 1.5 3.3 | awk '{print $1 + $2}'
4 Practical Examples of Variable Addition in Bash Scripts
In this section, I have listed some examples of the process of addition of bash variables which will help you to grow your bash variable addition skill in Bash script. This includes examples of arithmetic expansion, bc command, awk command, expr command, and command line argument approach.
Example 01: Using Arithmetic Expansion for Addition in Bash
An easy approach to addition is using arithmetic expansion. Here I will show you this straightforward approach where num1 and num2 are two variables added and saved to the sum variable. Then the sum variable is printed 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
- nano: Opens the nano text editor.
- script1.sh: Bash script name.
❸ Copy the script mentioned below:
#!/bin/bash
#setting value to num1 and num2
num1=10
num2=20
#addition of num1 and num2 variable directly
sum=$(($num1 + $num2))
#printing value of sum variable
echo $sum
#! /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=10 num2=20 command has assigned value to num1 and num2 variables. Subsequently the sum=$(($num1 + $num2)) command has calculated the addition of the num1 and num2 variables. Lastly, the echo $sum 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
- 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 that the Bash script has added the variables num1 and num2 with arithmetic expansion and printed the result on the terminal.
Example 02: Calculate Sum With Command Line Arguments
Here I have developed a Bash script which will calculate the sum of variable passed as command line arguments. To know more, follow the below script.
Script (script6.sh) >
#!/bin/bash
# Calculate the sum via command-line arguments
# $1 and $2 refers to the first and second argument passed as command-line arguments
# Calculating the summation of these two arguments
sum=$(( $1 + $2 ))
#printing the summation result
echo "Sum is: $sum"
The sum=$(( $1 + $2 )) command has added the two arguments passed to the Bash script. These $1 and $2 are positional parameters. After that, the echo “Sum is: $sum” command has printed the sum on the terminal.
Run the script by executing the following command
./script6.sh 5 7
The image shows that the summation of two variables is calculated with the help of the command line argument.
Example 03: Calculate Sum With Run Time Input
In this example, I will show you a bash script where the sum will be calculated with run time input. The num1 and num2 are two numbers run time input and these two variables will be added then printed on the terminal.
Script (script3.sh) >
#!/bin/bash
#take input from user and calculate sum.
read -p "Enter First Number: " num1
read -p "Enter Second Number: " num2
#adding variable num1 and num2
sum=$(( $num1 + $num2 ))
#printing value of sum
echo "Sum is: $sum"
The read -p “Enter First Number: ” num1; read -p “Enter Second Number: ” num2 commands have taken num1 and num2 run time input. Later on, the sum=$(( $num1 + $num2 )) command calculated the summation of the num1 and num2 variables and saved it to the sum variable. Finally, the echo “Sum is: $sum” command has printed the result on the terminal.
Run the script by executing the following command
./script3.sh
The image shows that the bash script has taken two run-time inputs, added them, and eventually printed the result on the terminal.
Example 04: Using Commands for the Addition of Bash Variable
There are some approaches to adding bash variables using commands. Three approaches including the expr command, the awk command, and the bc command are given below
1. Adding Two Variables With the “expr” Command in Bash Script
There are two approaches to using the expr command to add two variables with the expr command. The first approach is with single quotes and the syntax is
sum='expr $num1 + $num2'
And the second approach is without quotes and the syntax is
sum=$(expr $num1 + $num2)
Here I have developed a code to add num1 and num2 variables incorporating the second approach. To know more, follow the below script.
Script (script2.sh) >
#!/bin/bash
#setting value to num1 and num2
num1=40
num2=30
#addition of num1 and num2 variable with expr command
sum=$(expr $num1 + $num2)
#printing value of sum variable
echo $sum
The num1=40 num2=30 command has assigned value to num1 and num2 variables. Subsequently, the sum=$(expr $num1 + $num2) command has calculated the summation of these two variables. Finally, the echo command has printed the result on the terminal.
Run the script by executing the following command
./script2.sh
The image shows that the expr command has added two numbers then the echo command has printed it on the terminal.
2. Using “bc” to Add Floating-Point Numbers in Bash
The bc command means basic calculator. In this example, I will set two variables first, then add these two variables incorporating the pipe command and bc command. Follow the below script to know more.
Script (script4.sh) >
#!/bin/bash
#setting num1 and num2 variable
num1=3.1416
num2=1.5
#adding num1 and num2 with bc command associated with pipe command then print it
echo $num1 + $num2 | bc -l
The num1=3.1416 num2=1.5 command has assigned value to num1 and num2 variables. Then the echo $num1 + $num2 | bc -l command has passed the output of $num1 + $num2 command to the bc command and the bc command has calculated the summation of these two numbers. Finally, the echo command has printed the result on the terminal.
Run the script by executing the following command
./script4.sh
The image shows that the bc command has added the float numbers 3.1416 and 1.5 and the echo command has printed the result on the terminal.
3. Using “awk” to Add Floating-Point Numbers in Bash
The awk command command is helpful to do the mathematical addition without further compilation. In this example, I will pass two variables to the awk command incorporating the pipe command. Follow the below script to know more.
Script (script5.sh) >
#!/bin/bash
#two arguments are added utilizing the awk command and then printed on the terminal utilizing echo command
echo 1.5 3.3 | awk '{print $1 + $2}'
The echo 1.5 3.3 | awk ‘{print $1 + $2}’ command has passed 1.5 and 3.3 as $1 and $2 to the awk command which has added these two numbers by + sign. And finally, the echo command has printed the result.
Run the script by executing the following command
./script5.sh
The image shows that the awk command has added 1.5 and 3.3 and the echo command has printed the result on the terminal.
Conclusion
Here I have demonstrated some approaches to adding bash variables. I hope, after going through this article, you will be productive enough to do bash variable addition whenever 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]
- How to Subtract Two Bash Variables? [4+ Easy Approaches]
- How to Multiply Variable in Bash [6+ Practical Examples]
- Variable Substitution in Bash [Replace Character & Substring]
<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial