Addition of Bash Variables [4+ Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

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.

Add Two Variables in the Bash Scripts

To add two variables in bash, you can use several commands or approaches. Here are some methods to perform variable addition:

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

1. Using Arithmetic Expansion for Addition of Bash Variables

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 perform addition in Bash using arithmetic expansion, you can utilize the following script:

#!/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.

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.

2. Calculate Sum With Command Line Arguments

Command-line arguments in Bash are values provided to a script or command when it is executed in the terminal. These arguments allow you to pass information to the script or command during runtime.

To calculate the sum of command-line arguments in Bash, you can use the following script:

#!/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.

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.

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

To calculate the sum of numbers provided at runtime as user input, you can use the following Bash script:

#!/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.

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.

4. Using Commands for Addition of Bash Variables

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

A. 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 add two variables without using the quoting syntax, follow the script below:

#!/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.

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.

B. Adding Floating-Point Numbers in Bash Using “bc” Command

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.

To add floating-point numbers in bash using bc command, use the following script:

#!/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.

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.

C. Adding Floating-Point Numbers in Bash Using “awk” Command

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.

If you prefer to use awk for adding floating-point numbers in Bash, you can follow this example script:

#!/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.

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

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.

How do you add variables together in Bash?

To add variables together in Bash, you can use the ((…)) syntax for arithmetic expansion. Here’s an example:

#!/bin/bash

# Assuming num1 and num2 are variables with numeric values
num1=10
num2=5

# Add the values of num1 and num2 and store the result in a variable
result=$((num1 + num2))

# Print the result
echo "The sum of $num1 and $num2 is: $result"

What are functions called in Bash?

A bash function is a method, programmers 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.

What is “i++” in Bash?

In Bash, ++ is not used as an increment operator like it is in some other programming languages. Bash doesn’t have a direct increment operator like ++ or . However, you can increment a variable using other constructs.

Here’s an example using the let command:

#!/bin/bash

# Assuming i is a variable with a numeric value
i=5

# Increment i by 1
let "i++"

# Print the result
echo "The value of i after incrementing: $i"

In this example, let "i++" increments the value of the variable i by 1. After this operation, the script prints the updated value of i. You can use similar constructs with other arithmetic operations to achieve the desired increment behaviour in Bash.

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