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. In this article, I 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, join me as I demonstrate the art and science of adding 1 to a Bash variable.

Free Downloads

3 Practical Examples Related to Adding 1 to Bash Variable

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

Example 01: 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 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 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)) command has added 1 to the initial value of i and saved it to i. And finally, the echo $i command has printed the value of i 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

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 the i variable which is 12, then added 1 to that variable using +1 operator and printed 13 on the terminal.

Example 02: 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 I will print the initial value and final value of i 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
#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 has printed 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.

Run the script by executing the following command.

./script2.sh

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.

Example 03: 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 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
#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.

Run the script by executing the following command.

./script3.sh

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

I hope, after going through this article, you have gained some practical knowledge about adding 1 to bash variables and you 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 increment a Bash 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 do you add variables together in bash?

Bash allows its users to concatenate strings by writing strings one after the other or joining them using the += operator.

How to add strings in bash?

You can easily add two variables together by following the command: 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 - (8 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