Adding 1 to Bash Variable [3 Examples]

While Bash offers an extensive array of functions and features, it’s often the fundamental operations that form the building blocks of more complex scripts. This article will explore the straightforward way of incrementing the value of a variable by adding 1 to the variable. Whether you are a beginner eager to grasp the essential skill, Come forward as this article will demonstrate the art and science of adding 1 to a Bash variable.

3 Practical Examples of Adding 1 to Bash Variable

In this section, I have listed 3 practical examples related to adding 1 to the bash variable. I hope it will help you to build a solid foundation for adding 1 to the bash variable.

1. Add 1 to a Variable Using “+1” Operator in Bash Script

Here I will show you an example where I will add 1 to a variable using the +1 operator and then I will print the initial value and final value of the variable on the terminal.

To add 1 to a variable using the “+1” Operator, use the following script described below:

#!/bin/bash
#setting value to i variable
i=12
#printing value of i variable
echo $i
#increasing the value of the i variable
i=$((i+1))
#printing value of i variable
echo $i
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 i=12 command assigned a value to the i variable. Afterwards, the echo $i command has printed the value on the terminal. Then i=$((i+1)) , the command added 1 to the initial value of i and saved it to i. Finally, the echo $i command has printed the value of i on the terminal.

Firstly, the Bash script printed the initial value of the i variable which is 12, then added 1 to that variable using +1 operator and printed 13 on the terminal.Firstly, the Bash script printed the initial value of “i” variable which is 12, then added 1 to that variable using “+1” operator and printed 13 on the terminal.

2. Add 1 to a Variable Using “+=” Operator

Here I will show you an example where I will add 1 to a variable using += operator and then print the initial value and final value of “i” on the terminal.

To add 1 to a variable using the “+=” Operator, copy the following script given below:

#!/bin/bash
#setting value to i variable
i=10
#printing value of i variable
echo $i
#increasing the value of i variable
((i+=1))
#printing value of i variable
echo $i
EXPLANATION

the i=10 command assigned a value to the i variable. Afterward, the echo $i command prints the value on the terminal. Then ((i+=1)) command has added 1 to the initial value of i and saved it to i. And finally, the echo $i command has printed the modified value of i on the terminal.

Firstly, the Bash script printed the initial value of the i variable 10, then added 1 to that variable using +1 operator and printed 11 on the terminal.Firstly, the Bash script printed the initial value of the “i” variable 10, then added 1 to that variable using “+1” operator and printed 11 on the terminal.

3. Add 1 to a Variable Using “for” Loop

You can add 1 to a variable repetitively utilizing the for loop. Here I will develop a script where the value of the variable i will be incremented, and then the incremented value will be assigned to variable a.

To add 1 to a variable using “for” Loop, follow the below script:

#!/bin/bash
#initializing for loop
for ((i=0; i<5; ++i)); do
#printing the value of the variable i
echo $i
done
EXPLANATION

The for ((i=0; i<5; ++i); do command initialize the for loop with the value of the i variable 0 and increment it by one on every iteration. After that, the echo $i command has printed every value of i. After printing the value, the value of the i variable is incremented.

The variable's value has been increased from 1 to 5 one by one and printed every integer utilizing the for loop.The image shows that the variable’s value has been increased from 1 to 5 one by one and printed every integer utilizing the for loop.

Conclusion

After going through this article, I hope you have gained some practical knowledge about adding 1 to bash variables and will be able to implement this knowledge and skill to your bash script.

People Also Ask

How do I add 1 to a number variable in bash?

The ++ operator is a convenient way to add a Bash number variable using a single statement. It eliminates the need to explicitly specify the increment value because the operator increments its operand by 1 and returns the value.

How to increment a variable in Bash?

To increment a variable in Bash, you can use this syntax: ((number += 5)). It will increment the variable number by 5.

How do you add variables together in bash?

To add variables together, you can use the += operator. This operator concatenates string variables (writing strings one after the other or joining them).

How to add strings in bash?

To add strings together, use this syntax in the bash script: result=$variable_1 + $variable_2.

What is the advantage of bash script?

The bash script has some advantages like, it is simple to create and can run multiple commands easily. It has an Interactive debugging feature.

Related Articles


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

4.9/5 - (11 votes)
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
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