How to Increment a Variable in Bash?

Like other programming languages, bash script provides an environment or shell to execute a program in it. In this shell, you can utilize conditional statements, assign values to variables, and display output in terminal values. This guide will demonstrate different methods to increment a variable in the bash shell. The supported content of this guide is enlisted below:

Let’s start with the first one.

Method 1: Using + Operator to Increment a Variable in Bash

In Ubuntu, a new or existing shell file is required to perform some operations. For instance, a new “script.sh” file is created through the terminal by executing the below script:

$ sudo nano script.sh

It navigates to the nano editor on which we write code to increment a variable through the + operator.

$ i=10
$ echo $i
$ i=$(i+5)
$ echo $i

First, a variable “i” is carried out that stores a value of 10 and displays it through the “echo” statement. After that, increment a variable by adding five and storing them in the “i” variable. Finally, display the incremented value through the “echo” statement.

You can verify the incremented value “15” after the existing “script.sh” file in the Ubuntu terminal:

$ bash script.sh

Method 2: Using the += Operator to Increment a Variable in Bash

Another method can be considered to increment the variable by one. For this, the “+=” operator is used to increase the number one by one. The practical implementation of this operator is provided below: 

$ i=12
$ echo $i
$ ((i+=1)
$ echo $i

In the bash script, the “i” variable is utilized to store the value 12 and display it through the “echo” command. The “+=” operator is utilized for incrementing a variable by one and printing the updated output stored in the “i” variable.

$ bash script.sh

The output verifies that the current assigned number 12 has been incremented by 13.

Method 3: Using  Prefix Operator to Increment a Variable in Bash

Using the “++” prefix operator, you can also increment the variable value by one. For this, a code example is considered in which a variable stores a value of 12. After that, increment the variable by one through the prefix operator “++i”. Finally, print the output as “New Number” in the terminal:

$ i=12
$ echo Existing Number $((++i))
$ echo New Number $i

Next, execute the script:

$ bash script.sh

The prefix operator adds the number and then prints the output; therefore, the existing and new number has the same value, “13”. The output confirms that the prefix operator increments the variable “i” from 12 to 13.

Method 4: Using Postfix Operator to Increment a Variable in Bash

An alternative example can also be considered to increment the variable in bash via the postfix operator. To do so, the variable “i” acquires the 12 value and, after that, applies the postfix operator to this variable. Finally, print the output in the terminal as New Number through the “echo” command:

$ i=12
$ echo Existing Number $((i++))
$ echo New Number $i

After executing the command “bash script.sh” in the terminal, you can visualize the Existing Number containing 12 values and New Number having 13 values due to the postfix operator:

$ bash script.sh

The postfix operator will print the number first and then add value. Therefore, the existing and new numbers have different values.

Conclusion

In Linux, the “+”, “+=”, “Prefix”, and “Postfix” operators are utilized to increment a variable in the bash script. The bash can be executed by following the syntax “bash <filename.sh>” in the terminal. It is quite useful to increase the numeric values based on user requirements. This guide has explained all possible methods to increment a variable in bash.