Addition of Bash Variable [4+ Examples]

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.

Steps to Follow >

❶ 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

#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
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=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
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 Bash script has added the bash variables num1 and num2 with arithmetic expansion and printed the result on the terminal.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.

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

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
Note: Here 5 and 7 are input arguments.

The addition of two bash variables is calculated with the help of the command line argument.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.

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

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

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 bash script has taken two run-time inputs, added them, and eventually printed the result on the terminal.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.

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

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

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 expr command has done the addition of two bash variables then the echo command has printed it on the terminal.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.

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

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

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 bc command has added the float numbers 3.1416 and 1.5 and the echo command has printed the result on the terminal.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.

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

#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}'
EXPLANATION

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 awk command has added 1.5 and 3.3 and the echo command has printed the result on the terminal.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

What is the limitation of variable name in Bash?
Variable names can contain a sequence of alphanumeric characters and underscores. For variables created by you, the user, they should start with either an alphabetical letter or an underscore.
How do I increase the variable value in Bash?
You can increase variable value Using Increment (++) Operators in Bash. When using the ++ operator as a prefix like ++var. Then first the value of the variable is incremented by 1, and it returns the value. When using the ++ operator as a postfix like var++. Then first original value will returned and after that, the value incremented by 1.
What type of language is Bash?
Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions.
What are functions called in Bash?
A bash function is a method, programmer commonly use in shell scripts to group reusable code blocks. This feature is available for most programming languages, known under different names such as procedures, methods, or subroutines.

Related Articles


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

Rate this post
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