How To

How To Increment And Decrement Variables In Bash

Introduction

Incrementing and decrementing variables is a common task in Bash scripting. Increment means increasing a number by a specific value, usually by 1. Decrement means decreasing a number by a specific value, usually by 1.

If you are learning Bash scripting, this concept is very important because it is often used in loops, counters, retry scripts, menu programs, automation tasks, and cybersecurity scripts. For example, you may want to count login attempts, repeat a command five times, process numbered files, or create a countdown timer.

What Is Increment In Bash?

Increment means increasing the value of a variable.

Example:

number=1((number++))echo "$number"

Output:

2

Here, the value of number increases from 1 to 2.

What Is Decrement In Bash?

Decrement means decreasing the value of a variable.

Example:

number=5((number--))echo "$number"

Output:

4

Here, the value of number decreases from 5 to 4.

Basic Increment Example In Bash

Create a new Bash script:

nano increment.sh

Add the following code:

#!/bin/bashcount=1echo "Before increment: $count"((count++))echo "After increment: $count"

Save and run the script:

chmod +x increment.sh./increment.sh

Output:

Before increment: 1After increment: 2

The ((count++)) syntax increases the value by 1.

Basic Decrement Example In Bash

Create another script:

nano decrement.sh

Add this code:

#!/bin/bashcount=5echo "Before decrement: $count"((count--))echo "After decrement: $count"

Run it:

chmod +x decrement.sh./decrement.sh

Output:

Before decrement: 5After decrement: 4

Increment Variable By A Custom Value

You can also increase a variable by more than 1.

#!/bin/bashnumber=10number=$((number + 5))echo "New value: $number"

Output:

New value: 15

Another method:

((number += 5))

Decrement Variable By A Custom Value

To decrease a variable by a custom value, use:

#!/bin/bashnumber=20number=$((number - 5))echo "New value: $number"

Output:

New value: 15

Another method:

((number -= 5))

Increment Variable Inside A Loop

Incrementing is commonly used in loops.

#!/bin/bashcount=1while [[ $count -le 5 ]]do    echo "Count: $count"    ((count++))done

This script prints numbers from 1 to 5.

Decrement Variable Inside A Countdown

Decrementing is useful for countdown scripts.

#!/bin/bashcount=5while [[ $count -gt 0 ]]do    echo "Countdown: $count"    ((count--))doneecho "Done"

Conclusion

Incrementing and decrementing variables in Bash is simple and useful. You can use ((variable++)) to increase a value and ((variable--)) to decrease a value. You can also use $(( )) or ((variable += value)) for custom changes.

For beginners, learning increment and decrement operations is important because they are widely used in loops, counters, retry logic, automation scripts, and cybersecurity tasks.

Cyber Defence

Recent Posts

Install Apache Cassandra on Ubuntu 18.04: NoSQL Setup Guide

Apache Cassandra is a free, open-source NoSQL database designed for high availability and linear scalability with…

23 minutes ago

Install Rocket.Chat on Ubuntu 18.04 with Nginx and Let’s Encrypt

Rocket.Chat is a free, open-source team communication platform built with the Meteor framework. It is a…

28 minutes ago

Install MySQL on Ubuntu 18.04: Setup, Security, and Root Access

MySQL is the most popular open-source relational database management system. It is fast, reliable, and scales…

32 minutes ago

Install Apache on Ubuntu 18.04: Web Server Setup and Config

Apache is the most widely used web server in the world. It is free, open-source, and…

37 minutes ago

Install NetBeans IDE on Ubuntu 18.04 with Snap and OpenJDK 8

NetBeans is a free, open-source, cross-platform IDE developed by the Apache Software Foundation. It was one…

41 minutes ago

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

5 days ago