Increment Variable Value in Bash Scripts [4+ Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The increment of the Bash variable means increasing the value of a variable. It is an important technique for the Bash programmer to do a specific task repetitively. Here, I will explore the techniques of pre-increment and post-increment using the for loop, the while loop, and the until loop. After reading this article, I hope you will be skillful enough to increment the Bash variable.

What is Increment in Bash?

The increment is mainly the operation of increasing the value of a variable. It is an important tool for bash programmers. You can increase variable value by one, two, three, four, etc., according to your necessity. There are two types of increments in Bash, such as:

  • Post-increment: In post-increment, at first, the variable’s value is returned, then the value is increased. The syntax for pre-increment is:
    ++variable
  • Pre-increment: In pre-increment, at first, the variable’s value is increased, and then the value is returned. The syntax for post-increment is:
    variable++

Operators to Increment Bash Variable

There are some operators used to increment the bash variable. A list of some of these operators is given below:

Operators Function
+ Basic operator for increment. An example of this operator is variable = variable + 1.
++ An example of this operator is ++variable.
+= An example of this operator is variable+=1.

How to Increment a Variable in Bash?

To increment a variable in Bash, you can use the ((var++)) syntax, which increases the variable value by one. Here’s a simple example:

var=1
((var++))
echo $var

Output: 2
In this example, var starts at 1, and ((var++)) increments it by one. The echo $var command prints the updated value, which is 2. This is the simplest method to increment a bash variable, but there are some more practical approaches. Read the following article for more details.

4 Examples Showing the Increment of Bash Variable

Incrementing the bash variable is a necessary operation for looping, counting, progress tracking, and conditional operations. These operations are necessary for data manipulation and automate complex tasks.

Example 01: Increment the Variable in Bash

You can do post-increment by using the x++ operator and pre-increment by using the ++x operator. In the following two cases, I will discuss each of the processes in detail:

Case 01: Using “x++” Syntax to Post Increment Variable in Bash

In post-increment operation, increment operation can be done after the main task. Here, I will develop a script where the value of variable x will be assigned to variable a, and then the value of variable x will be incremented.

To post-increment the variable using the “x++” syntax, see the below Bash script:

#!/bin/bash
#assigning value to a
x=10
#post increment of the variable a
a=$((x++));
#printing the value of a
echo $a
EXPLANATION

In the first script line, #! /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 x=10 command assigned a value to the x variable. Afterward, the a=$((x++)); command has post-incremented the x variable and assigned it to a variable. Finally, the echo $a command has printed the value of the variable on the terminal.

The post-increment has been done on the variable using the x++ operation and so first the initial value 10 is printed on the terminal then the value is increased from 10 to 11.The image shows that post-increment has been done on the variable. First, the initial variable value of 10 is printed on the terminal then the value is increased from 10 to 11.

Case 02: Using “++x” Syntax for Pre Increment Variable in Bash

In pre-increment operation, increment operation can be done before the main task. Here I will develop a script where the value of variable x will be incremented, and then the incremented value will be assigned to variable a. To know more, follow the below script:

#!/bin/bash
#assigning value to a
x=10
#pre increment of the variable a
a=$((++x));
#printing the value of a
echo $a
EXPLANATION

The x=10 command assigned a value to the x variable. Afterward, the a=$((++x)); command has pre-incremented the x variable and assigned it to the a variable. Finally, the echo $a command has printed the value of the variable on the terminal.

The pre-increment has been done on the variable using the ++x increment operator and so the value is increased first from 10 to 11 then the value is printed on the terminal.The image shows that pre-increment has been done on the variable, so the value is increased first from 10 to 11 and then printed on the terminal.

Example 02: Increment Variable Value With “for” Loop

In this section, I will show you the process of incrementing variables using the for loop. In the first case, I will show you pre-increment, and in the second case, I will show you post-increment.

Case 01: Using “for” Loop to Perform Pre-increment of Variable

In pre-increment operation, increment operation can be done before the main task utilizing the bash for loop. Here I will develop a script where the value of the variable x will be incremented, and then the incremented value will be assigned to variable a. To know more, 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 will print every value of i.

The pre-increment has been done on the variable utilizing the for loop with ++ increment operator.Upon execution, the script prints pre-incremented values on the terminal.

Case 02: Using “for” Loop to Perform Post-Increment of Variable

In post-increment operation, increment operation can be done after the main task utilizing the for loop. Here, I will develop a script where the value of variable x will be assigned to variable a, and then the value of variable x will be incremented. To know more, 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 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 post-increment has been done on the variable utilizing the for loop with i++ increment.The image shows that post-increment has been done on the variable utilizing the for loop with i++ increment.

Example 03: Increment Variable Value With “while” Loop

The while loop is another approach to increment variables. Here, I will briefly show you two cases of increasing variable value with the while loop. The first one is by using += notation, and the second one is by using +1 notation.

Case 01: Incrementing a Variable Using “while” Loop With “+=” Notation

You can increment a variable using the while loop with += notation. Here, I will accomplish this by developing a Bash script. To know more, follow the below script:

#!/bin/bash
i=0
while [ $i -lt 5 ]
do
echo $i
let “i+=1”
done
EXPLANATION

The i=0 command assigned the value of i equal to zero. Then, the while [ $i -lt 5 ] command initiates the while loop and checks whether i is less than 5, and if the condition is true, it passes a zero status and thus executes the echo $i command. Then, the let “i+=1” command increments the value of i by one. Finally, done denotes the end of the while loop.

The value of the variable i has been increased one by one using the while loop with += notation.See from the image that the value of the variable i has been increased one by one.

Case 02: Incrementing a Variable Using “while” Loop With “+1” Notation

You can increment a variable using the while loop with +1 notation. Here, I will accomplish this by developing a Bash script. To see the process practically, execute the below script:

#!/bin/bash
i=0
while [ $i -lt 5 ]
do
echo $i
let “i=i+1”
done
EXPLANATION

The i=0 command assigned the value of i equal to zero. Then the while [ $i -lt 5 ] command initiates the while loop and checks whether i is less than 5, and if the condition is true it passes a zero status and thus executes the echo $i command. Then, the let “i+=1” command increments the value of i by one. Finally, done denotes the end of the while loop.

The value of the variable i has been increased one by one using the while loop with +1 notation.The image shows that the value of the variable i has been increased one by one using the while loop with +1 notation.

Example 04: Increment Variable in Bash Using “Until” Loop

The until loop is a different type of loop, It passes a zero return value until the condition is true. Thus, a command inside the loop is executed. Here, I have a bash script that has a until loop inside it and does an increment operation. See the below script to check the entire process:

#!/bin/bash
#value of the i has been assigned
i=0
#until loop will execute the echo command inside it until i value is greater than 5
until [ $i -gt 5 ]
do
#value of i will be printed on the terminal
echo $i
#value of i will be increased by one and will be saved on the i variable
i=$((i+1))
done
EXPLANATION

The i=0 command has assigned zero as a value of i. After that the until [ $i -gt 5 ] command initiates the until loop and executes the inside command until the value of i is not greater than 5. Inside the loop, the echo $i command prints the value of the i variable on the terminal. Afterwards, the<strong> i=$((i+1))</strong> command increments the value of the i variable by one. Finally, the done command denotes the end of the until loop.

The integer from 0 to 5 has been printed on the terminal using the until loop with +1 operation.See from the image, the script prints integers from 0 to 5 on the terminal.

Conclusion

In this article, I have shown you some techniques to pre-increment and post-increment the bash variables utilizing for loop, while loop, and until loop. These discussed cases and examples will help you to do bash variables more productively.

People Also Ask

How do you increment a variable in bash?

To increment a variable in bash, you can use different syntaxes. Some of these command syntaxes are variable = variable + 1, ++variable, variable+=1, etc.

How do you increment a function in bash?

To increment a variable within a function in bash, you can use the ++ operator which increments its operand by 1 and returns the value. The operator can be used before or after the operand. They are also known as prefix increments: ++i.

How do you set a variable to increment?

To increment a variable number each time, you can add 1 to the variable using the increment operator ++. This will increase the variable’s value by 1 each time it is called. For example, each time num++ is called, it increments the value of num by 1.

Can a variable be replaced by any value?

Yes, you can, and often do, replace the old value of a variable based on the current value of the variable.

Related Articles


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

5/5 - (7 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