[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedApril 10, 2023

How to Increment and Decrement Variable in Bash

How To Increment And Decrement Variable In Bash

One of the most common arithmetic operations when writing Bash scripts is incrementing and decrementing variables. This is most often used in loops as a counter, but it can occur elsewhere in the script as well.

Incrementing and Decrementing means adding or subtracting a value (usually 1), respectively, from the value of a numeric variable. The arithmetic expansion can be performed using the double parentheses ((…)) and $((…)) or with the let built-in command.

In Bash, there are multiple ways to increment/decrement a variable. This article explains some of them.

 

Using + and Operators

The most simple way to increment/decrement a variable is by using the + and operators.

i=$((i+1))
((i=i+1))
let "i=i+1"
i=$((i-1))
((i=i-1))
let "i=i-1"

This method allows you increment/decrement the variable by any value you want.

Here is an example of incrementing a variable within an until loop:

i=0

until [ $i -gt 3 ]
do
  echo i: $i
  ((i=i+1))
done
i: 0
i: 1
i: 2
i: 3

 

The += and -= Operators

In addition to the basic operators explained above, bash also provides the assignment operators += and -=. These operators are used to increment/decrement the value of the left operand with the value specified after the operator.

((i+=1))
let "i+=1" 
((i-=1))
let "i-=1" 

In the following while loop, we are decrementing the value of the i variable by 5.

i=20

while [ $i -ge 5 ]
do
  echo Number: $i
  let "i-=5" 
done
Number: 20
Number: 15
Number: 10
Number: 5

 

Using the ++ and Operators

The ++ and operators increment and decrement, respectively, its operand by 1 and return the value.

((i++))
((++i))
let "i++"
let "++i"
((i--))
((--i))
let "i--"
let "--i"

The operators can be used before or after the operand. They are also known as:

  • prefix increment: ++i

  • prefix decrement: –i

  • postfix increment: i++

  • postfix decrement: i–

The prefix operators first increment/decrement the operators by 1 and then return the new value of the operators. On the other hand, the postfix operators return the operators` value before it has been incremented/decremented.

If you only want to increment/decrement the variable, then there is no difference if you use the prefix or postfix operator. It makes a difference only if the result of the operators is used in some other operation or assigned to another variable.

The following examples demonstrates how the ++ operator works when is used before and after its operant:

x=5
y=$((x++))
echo x: $x
echo y: $y
x: 6
y: 5
x=5
y=$((++x))
echo x: $x
echo y: $y
x: 6
y: 6

Below is an example of how to use the postfix incrementor in a bash script:

#!/bin/bash
i=0
while true; do
  if [[ "$i" -gt 3 ]]; then
       exit 1
  fi
  echo i: $i
  ((i++))
done

The disadvantage of using these operators is that the variable can only be incremented or decremented by 1.

 

Conclusion

Incrementing and decrementing variables in Bash can be performed in many different ways. Whatever method you use, the result is the same.

Keep reading
How to use unmask command.
April 12, 2023

How to use unmask command.

How To Use Unmask Command. It is used by mkdir, touch, tee , and other commands that create new files and directories. On Linux and Unix operating systems, all new files are created with a default set of permissions. The umask utility allows you to view or to set the file mode creation mask, which…

Read article
How to Check if a String Contains a Substring in Bash
April 12, 2023

How to Check if a String Contains a Substring in Bash

How To Check If A String Contains A Substring In Bash In this article, we will show you several ways to check if a string contains a substring. One of the most common operations when working with strings in Bash is to determine whether or not a string contains another string. Using Wildcards The…

Read article
How to Check for Open Ports
April 12, 2023

How to Check for Open Ports

How To Check For Open Ports This article describes several approaches to find out what ports are opened to the outside on your Linux system. Whether you are troubleshooting network connectivity issues or configuring a firewall, one of the first things to check is what ports are actually opened on…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00