How to Increment and Decrement Variable in Bash

June 1, 2023

Introduction

In Bash scripting, variables are essential for storing and manipulating data. They serve as placeholders for different types of values, such as numbers, strings, arrays, etc.

Increment or decrement the values of the variables during script execution to perform various calculations and track changes in your program.

In this tutorial, you will learn to increment and decrement a variable in Bash.

How to increment and decrement a variable in Bash - a tutorial.

Prerequisites

  • A system running Linux.
  • Access to the terminal.

Incrementing a Variable in Bash

Incrementing a variable means increasing its value by a certain amount, typically by adding a number or by increasing it by 1. It is often used for counting, iterating through loops, or tracking progress.

Note: See how to print a variable in Bash using printf.

Increment Bash Variable with + Operator

Most programming languages allow using the + operator to increment a variable's value. Increment a variable by summing it with any value. By default, the $((…)) operators denote the use of the + operator in an arithmetic operation.

Follow the steps below to create a script and view the process:

1. Open the terminal (Ctrl + Alt + T) and create a new Bash script. Use a text editor such as vi/vim:

vi script.sh

2. Enter the following code:

#!/bin/bash 
i=12
echo $i
i=$((i+1))
echo $i

Here, ((i+1)) increments the i variable by 1.

3. Save the script and exit vi:

:wq

4. Run the Bash script to test if it works:

bash increment.sh
Testing the + operator for incrementing a variable in Bash.

The output shows that the i variable value has been incremented by 1, from 12 to 13.

Increment Bash Variable with += Operator

The += operator in Bash also increments a variable by a specified amount. It is a shorthand notation for the sum operator, where the first operand and the result variable name are the same and assigned in a single statement.

Follow the steps below to test the += operator and increment a variable:

1. Create a new Bash script using a text editor.

2. Enter the following code:

#!/bin/bash 
i=10
echo $i
((i+=1))
echo $i

In this case, ((i+=1)) increments the i variable by 1.

3. Save the script and run it:

Incrementing a Bash variable with the += operator.

The variable value has been incremented by 1, from 10 to 11, as seen in the output.

Increment Bash Variable with ++ Operator

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. Use the ++ operator directly with the variable.

Follow the steps below to test the ++ operator in a Bash script:

1. Create a new Bash script.

2. Paste the following code:

#!/bin/bash
i=10
echo $i
echo $((i++))
echo $i
echo $((++i))
echo $i
((i++))
echo $i

In this example:

  • $((i++)) increments the value of i by 1 but returns the previous value of i before the increment.
  • echo $((i++)) prints the value of i before the increment.
  • echo $i prints the updated value of i after the increment.
  • $((++i)) increments the value of i by 1 and returns the updated value.
  • echo $((++i)) prints the value of i after the increment.

The i++ and ++i operators increment the variable in different ways and the $(( )) construct is used for arithmetic evaluation.

4. Save the script and run it:

Incrementing a variable in Bash using the ++ operator.

The script initializes the variable i with the value 10, increments it in two different ways, and prints the intermediate and final values.

Decrementing a Variable in Bash

Decrementing a variable means reducing its value by a certain amount, usually by subtracting a specific number, i.e., by decreasing it by 1. It is commonly used in scripting to iterate through a loop or track a decreasing count.

Decrement Using the - Operator

The simplest way to decrement a variable in Bash is to use the - operator. Follow the steps below to decrement a variable in Bash using an until loop:

1. Create a new Bash script.

2. Paste the following code:

#!/bin/bash
count=5
until [ $count -le 0 ]
do
echo count: $count
((count=count-1))
done

In the above code:

  • count=5 declares a variable named count and assigns it an initial value of 5.
  • until is a loop control structure in Bash that executes the loop as long as the condition [ $count -le 0 ] is false. In this case, the loop continues until the value of count is less than or equal to 0.
  • ((count=count-1)) decrements the value of count by 1. It subtracts 1 from the current value of count and assigns the result back to count.

3. Save and run the script:

Decrementing a variable in Bash using the - operator.

The script decrements the variable value in the loop until the set condition is true.

Decrement Using the -= Operator

Aside from the basic - operator, Bash also supports the -= operator. This operator decrements the value of the left operand with the value specified after the operator.

Note: See how to use bash break to exit from a loop, or bash continue to resume a loop.

Follow the steps below to test the -= operator in a while loop:

1. Create a new Bash script.

2. Paste the following code:

#!/bin/bash
i=20
while [ $i -ge 5 ]
do
  echo Number: $i
  let "i-=5" 
done

In the above code:

  • i=20 declares a variable named i and assigns it an initial value of 20.
  • while continues executing the loop as long as the condition [ $i -ge 5 ] is true. In this case, the loop will continue until the value of i is greater than or equal to 5.
  • let "i-=5" decrements the variable value by 5 and assigns the result back to i. The let command is a shorthand notation for performing arithmetic operations in Bash.

3. Save and run the script:

Decrementing a variable value by 5 using the -= operator.

The script decrements the variable value by 5 until the value equals 5.

Decrement Using the -- Operator

The -- operator also decrements variable values. However, it decrements the variable only by 1, unlike other operators that accept other values. You can use -- as prefix or postfix operators, which means you can add them immediately before or after the operator.

Follow the steps below to decrement a variable using --:

1. Create a new Bash script.

2. Paste the following code:

#!/bin/bash
counter=10
((counter--))
echo "Counter: $counter"

In this example:

  • counter=10 declares a variable named counter and assigns it an initial value of 10.
  • ((counter--)) decrements the value of counter by 1 using the -- operator. It subtracts 1 from the current value of counter.
  • echo "Counter: $counter" prints the current value of counter to the console.

3. Save and run the script:

Decrementing a variable using the -- operator.

The script decrements the variable by 1 and prints the result.

Prefix and Postfix

The difference between prefix and postfix notation when incrementing or decrementing a variable in Bash is the order and timing of the operations.

  • Prefix. The operator is placed before the variable. For example, ++i or --i. In this case, it increments/decrements the variable before evaluating or assigning the expression.
  • Postfix. The operator is placed after the variable. For example, i++ or i--. In this case, it increments or decrements the variable after evaluating or assigning the expression.

If your aim is only to increment/decrement the variable, then there is no difference if you use the prefix or postfix operator. The distinction between the two operators is relevant only when the result of the operation is utilized in subsequent operations or assigned to another variable.

The following example shows the difference between using prefix and postfix operators:

1. Create a new Bash script and paste the following code:

#!/bin/bash
counter=5
result_postfix=$((counter++))
result_prefix=$((++counter))
echo "Postfix result: $result_postfix"
echo "Prefix result: $result_prefix"
echo "Updated counter value: $counter"

In the above code:

  • counter=5 declares the counter variable and assigns it an initial value of 5.
  • result_postfix=$((counter++)) uses the postfix operator ++ to increment the value of counter after assigning its current value to result_postfix. The result of this expression is the original value of counter (5), which is then assigned to result_postfix.
  • result_prefix=$((++counter)) uses the prefix operator ++ to increment the value of counter before assigning its current value to result_prefix. The result of this expression is the incremented value of counter (7), which is then assigned to result_prefix.
  • echo "Postfix result: $result_postfix" prints the value of result_postfix.
  • echo "Prefix result: $result_prefix" prints the value of result_prefix.
  • echo "Updated counter value: $counter" prints the updated value of counter.

2. Save the script and run it:

Testing the difference between prefix and postfix operators in Bash.

The postfix operator (counter++) returns the current value of counter before the increment, while the prefix operator (++counter) increments the value before the assignment.

Conclusion

After reading this tutorial, you should know how to increment and decrement a variable in Bash. Use the supported operators and elevate your scripting capabilities when iterating through loops or counts in a script.

Next, check out our tutorial for the Bash if elif else statement, or learn to use the Bash case statement effectively.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
Bash Script for Loop Explained with Examples
December 15, 2021

This step-by-step article shows how to use the for loop in bash scripts through various hands-on examples.
Read more
Bash Function & How to Use It {Variables, Arguments, Return}
November 3, 2021

Learn how to use functions in Bash scripting through this hands-on tutorial with example codes.
Read more
How To Customize Bash Prompt in Linux
May 12, 2020

Follow this article to learn how to make changes to your BASH prompt. The guide shows how to edit the bashrc file, as well as how to modify PS1 variables.
Read more
Bash wait Command with Examples
September 23, 2021

The wait command helps control the execution of background processes. Learn how to use the wait command through bash scripts.
Read more