How to Increment and Decrement Variable in Bash?

Increment or decrement variable is a popularly used arithmetic operation in many programming languages. The increment adds a number, and the decrement subtracts a number programmatically. The bash script allows the incrementing or decrementing of the variables, mostly used with the loops as a counter, but it can be used within the script to increase or decrease the variable value.

This guide will explain different methods for incrementing and decrementing variables in bash with the following timeline:

How to Increment or Decrement a Variable in Bash?

Several bash syntaxes and operators are used to increment and decrement variables. Let’s check the syntax and operators with the help of examples.

Syntax for Increment and Decrement Variable

The increment or decrement is done using the three formats:

  • Double brackets (())
  • Declaring a variable with double brackets $(())
  • Using the let “”

For instance, to increment the variable var, the below syntaxes are used:

$ ((var=var+1))
((var=var+1))
let "var=var+1"

Similarly, to decrement the variable var, the following syntaxes can be executed:

$ ((var=var-1))
((var=var-1))
let "var=var-1"

Type of Increment and Decrement Operators in Bash

The following operators are used for incrementing or decrementing the variables in bash:

+ operator – operatorvar=var+1 var=var-1
++ operator — operatorvar++ ++var var– –var
+= operator +- operatorvar+=1 var-=1

How to Use Bash to Increment or Decrement Variables?

This section will discuss several methods to decrement or increment variables.

Example 1: Increment the Variable in Bash

We can increment the variables in Bash by declaring it within curly brackets. This method is commonly used in Linux to increment the variable. For instance, to increment the variable by 1, use the below increment code:   

#!/bin/bash
var=5
var=$((var+1))
echo "The incremented values is: $var"

To check the above bash script output, use this command:

$ ~/increment.sh

The output displays that the variable var=5 is incremented to 6.

The variable in bash can be incremented using the double curly brackets only. To increment the variable var by 1, use this code:

#!/bin/bash
var=10
((var=var+1))
echo "The incremented values is: $var"

To execute the above increment code, run this command:

$ ~/increment.sh

The variable var=10 is added by 1 is shown in the output.

The let command is also used to increment in the Bash script. To increment the variable var using the let syntax, execute this code:

#!/bin/bash
var=15
let "var=var+1"
echo "The incremented values is: $var"

Run the above code with this command:

$ ~/increment.sh

The variable is incremented to 16 from 15.

Example 2: Increment the Variable in Bash Using While Loop

The loops automate the tasks in the bash script. For instance, to increment the variable using the while loop, execute this bash script:

#!/bin/bash
i=0
while(($i<=5))
do
   ((i++))
   echo $i
done

For running the bash script, use:

$ ~/increment.sh

The output shows that 1 is added to the variable every time the loop executes.

Example 3: Increment the Variable in Bash Using Until Loop

The until the loop can also be used to increment a variable. The until loop increments the variable by 1 every time the loop runs and terminates the loop at the maximum set point, as performed below:

#!/bin/bash
i=0
until [ $i -gt 5 ]
do
  echo $i
  i=$((i+1))
done

To run the bash script, execute:

$ ~/increment.sh

The variable is incremented by 1 from 0 to 5.

How to Decrement a Variable in Bash?

As discussed above, we can decrement the bash variable using the three operators –, -+ and -.

Example 1: Decrement the Variable in Bash

To decrement the variable by 1, use this below declaring variable bash script:

#!/bin/bash
var=5
var=$((var-1))
echo "The decremented value is: $var"

Run the decrement bash script code to get the output:

$ ~/decrement.sh

The variable var=5 is decremented to 4.

The curly braces syntax can also be used to decrement a variable. For decrementing a variable var=10 by 1, use:

#!/bin/bash
var=10
((var=var-1))
echo "The decremented value is: $var"

The decrement bash script code can be executed using:

$ ~/decrement.sh

The variable var=10 is decreased by 1 to 9.

The let syntax is used to decrement a variable in bash using this script:

#!/bin/bash
var=15
let "var=var-1"
echo "The decremented value is: $var"

To check the decrement bash script output, run this command:

$ ~/increment.sh

The variable is decremented by 1.

Example 2: Decrement the Variable in Bash Using While Loop

The below while loop is used to decrement the variable by 1 every tie the loop runs:

#!/bin/bash
i=5
while(($i>0))
do    echo $i
   ((i--))
done

Run the while loop bash script, running this command in the terminal:

$ ~/decrement.sh

Example 3: Decrement the Variable in Bash Using Until Loop

The until is used to decrement a variable for each loop execution, such as the below code will decrement the variable i by 1:

#!/bin/bash
i=5
until [ $i -lt 1 ]
do
  echo $i
  i=$((i-1))
done

To get the output for the above script, use:

$ ~/decrement.sh

The script is decremented by 1 for every loop execution.

Example 4: Decrement the Variable by 3 in Bash Using Until Loop

Let’s decrement a loop using the let syntax with the below script, which will decrement the loop by 3 for every time the loop executes:

#!/bin/bash
i=12

while [ $i -ge 3 ]
do
  echo $i
  let "i-=1"
done

To run the decrement bash script, execute:

$ ~/decrement.sh

The output is decremented by 3 for every loop execution.

This guide has explained with examples that how can we increment and decrement variables in Bash.

Conclusion

Three different Linux operators and syntaxes are used to increment or decrement in bash. The syntaxes used for incrementing or decrementing a variable are curly brackets (()), declaring a variable within curly brackets $(()), and using the let keyword in the bash script. Moreover, three operators, +, ++, and += are used to increment, and -, –, and -= are utilized to decrement a variable using the bash script. So, this blog has covered in detail how can we increment and decrement variables in Bash.