If you are writing a bash script and you want to increment a counter variable, below are five ways in which you can do it.
Option 1: Using += operator
#!/bin/bash
counter_variable=0
((counter_variable+=1))
echo $counter_variable
Output:
Option 2: Using let command
#!/bin/bash
counter_variable=0
let "counter_variable=counter_variable+1"
echo $counter_variable
Output:
Option 3: Using arithmetic expansion
#!/bin/bash
counter_variable=2
((counter_variable++))
echo $counter_variable
Output:
Option 4: Using expr command
#!/bin/bash
counter_variable=5
counter=$(expr $counter_variable + 1)
echo $counter_variable
Output:
Option 5: Using (( )) construct
#!/bin/bash
counter_variable=9
((counter_variable++))
echo $counter_variable
Output:

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Bash,
- Bash Hello World! Script Tutorial
- How to run bash command in background
- How to create new user account in Windows bash
- How to Compare Strings in Bash
- Bash: Allow Command to Fail without exiting Script
- Fix bash: script.sh: Permission denied Error
- Bash getopts Command Example
- How to fix bash ping command not found error
- How to add NewLine Character in Bash Script String
- How to Echo Bash Command to a File
- Bash Command to Find String in a File
- How to open new Terminal using Bash Command
- Bash How to Save Output of a Command to a Variable
- Download a SSL Certificate from a URL in Terminal
- Bash Command to Check IP Address
- Know Bash shell version command
- Bash Command To Check If File Exists
- How to run a Command in Bash Script
- How to Compare two Files in Bash Shell
- How to check if a variable is set in Bash Script or Not
- Convert String from uppercase to lowercase in Bash
- How to see Created Accessed Modified and Changed dates of a file using bash terminal command
- Bash Command To Get Current Time
- Bash command to List Files
- Fix: bash: syntax error near unexpected token
More Posts:
- Java: Testing Private Methods in JUnit using reflection API Example - Java
- Connection Failed: 1130 PHP MySQL Error - MySQL
- 3 Python program to add two numbers - Python
- How to install wget on macOS - MacOS
- How to Request a Refund for Apple Apps and Games - Apple
- 21 Useful Android Emulator Short-cut Keyboard Keys - Android
- Fix- Microsoft Word Pages Appear Black - Microsoft
- Fix: zsh: command not found: npm after installation (Node.js) - zsh