The increment of the Bash variable means increasing the value of a variable. It is an important technique for the Bash programmer to do a specific task repetitively. Here, I will explore the techniques of pre-increment and post-increment using the for loop, the while loop, and the until loop. After reading this article, I hope you will be skillful enough to increment the Bash variable.
Key Takeaways
- Learning about utilizing for loop, while loop, and until loop to increment bash variable.
- Getting familiar with the post-increment and pre-increment process of variables in bash scripts.
Free Downloads
Increment in Bash
The increment is mainly the operation of increasing the value of a variable. It is an important tool for bash programmers. You can increase variable value by one, two, three, four, etc., according to your necessity.
Types
- Post-increment: In post-increment, at first, the variable’s value is returned, then the value is increased.
- Pre-increment: In pre-increment, at first, the variable’s value is increased, and then the value is returned.
Syntax
Syntax for pre-increment is
++variable
And syntax for post-increment is
variable++
Operators
There are some operators to increment the bash variable. Some operators are given below.
Operators | Function |
---|---|
+ | It is a basic operator for increment. An example of this operator is variable = variable + 1. |
++ | An example of this operator is ++variable. |
+= | An example of this operator is variable+=1. |
4 Examples Related to the Increment of the Bash Variable
Incrementing the bash variable is a necessary operation for looping, counting, progress tracking, and conditional operations. These operations are necessary for data manipulation and automate complex tasks.
Example 01: Increment the Variable in Bash
You can do post-increment by using the x++ operator and pre-increment by using the ++x operator. In two cases, I will now show you it briefly.
Case 01: Using the x++ Syntax for Post Increment the Variable in Bash
In post-increment operation, increment operation can be done after the main task. Here, I will develop a script where the value of variable x will be assigned to variable a, and then the value of variable x will be incremented. To know more, follow the below steps.
❶ At first, launch an Ubuntu Terminal.
❷ Write the following command to open a file in Nano.
nano script1.sh
- nano: Opens the nano text editor.
- script1.sh: Bash script name.
❸ Copy the script mentioned below:
#!/bin/bash
#assigning value to a
x=10
#post increment of the variable a
a=$((x++));
#printing the value of a
echo $a
#! /bin/bash ‘#!’, is called shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash. After that, the x=10 command assigned a value to the x variable. Afterward, the a=$((x++)); command has post-incremented the x variable and assigned to the a variable. Finally, the echo $a command has printed the value of the variable on the terminal.
❹ Press CTRL+O and ENTER to save the file; CTRL+X to exit.
❺ Use the following command to make the file executable:
chmod +x script1.sh
- chmod: Changes the permissions of files and directories.
- +x: Argument with the chmod command to add the executable permission.
- script1.sh: File that you want to make executable.
❻ Run the script by the following command:
./script1.sh
The image shows that post-increment has been done on the variable using the x++ operation and so first the initial value 10 is printed on the terminal then the value is increased from 10 to 11.
Case 02: Using ++x Syntax for Pre Increment the Variable in Bash
In pre-increment operation, increment operation can be done before the main task. Here I will develop a script where the value of variable x will be incremented, and then the incremented value will be assigned to variable a. To know more, follow the below script.
Script (script2.sh) >
#!/bin/bash
#assigning value to a
x=10
#pre increment of the variable a
a=$((++x));
#printing the value of a
echo $a
The x=10 command assigned a value to the x variable. Afterward, the a=$((++x)); command has pre-incremented the x variable and assigned it to the a variable. Finally, the echo $a command has printed the value of the variable on the terminal.
Run the script by executing the following command
./script2.sh
The image shows that pre-increment has been done on the variable using the ++x increment operator and so the value is increased first from 10 to 11 then the value is printed on the terminal.
Example 02: Increment Variable Value with the for Loop
In this section, I will show you the process of incrementing variables using the for loop. In the first case, I will show you pre-increment, and in the second case, I will show you post-increment.
Case 01: Using for Loop to Perform Pre-increment of a Variable
In pre-increment operation, increment operation can be done before the main task utilizing the for loop. Here I will develop a script where the value of the variable x will be incremented, and then the incremented value will be assigned to variable a. To know more, follow the below script.
Script (script3.sh) >
#!/bin/bash
#initializing for loop
for ((i=0; i<5; ++i)); do
#printing the value of the variable i
echo $i
done
The for ((i=0; i<5; ++i); do command initialize the for loop with the value of the i variable 0 and increment it by one on every iteration. After that, the echo $i command has printed every value of i.
Run the script by executing the following command
./script3.sh
The image shows that pre-increment has been done on the variable utilizing the for loop with ++ increment operator.
Case 02: Using for Loop to Perform Post-Increment of a Variable
In post-increment operation, increment operation can be done after the main task utilizing the for loop. Here, I will develop a script where the value of variable x will be assigned to variable a, and then the value of variable x will be incremented. To know more, follow the below script.
Script (script4.sh) >
#!/bin/bash
#initializing for loop
for ((i=0; i<5; i++)); do
#printing the value of the variable i
echo $i
done
The for ((i=0; i<5; i++); do command initialize the for loop with the value of i variable 0 and increment it by one on every iteration. After that, the echo $i command has printed every value of i. After printing the value, the value of the i variable is incremented.
Run the script by executing the following command
./script4.sh
The image shows that post-increment has been done on the variable utilizing the for loop with i++ increment.
Example 03: Increment Variable Value with while Loop
The while loop is another approach to increment variables. Here, I will briefly show you two cases of increasing variable value with the while loop. The first one is by using += notation, and the second one is by using +1 notation.
Case 01: Incrementing a Variable using “while” Loop with “+=” Notation
You can increment a variable using the while loop with += notation. Here, I will accomplish this by developing a Bash script. To know more, follow the below script.
Script (script5.sh) >
#!/bin/bash
i=0
while [ $i -lt 5 ]
do
echo $i
let “i+=1”
done
The i=0 command assigned the value of i equal to zero. Then, the while [ $i -lt 5 ] command initiates the while loop and checks whether i is less than 5, and if the condition is true, it passes a zero status and thus executes the echo $i command. Then, the let “i+=1” command increments the value of i by one. Finally, done denotes the end of the while loop.
Run the script by executing the following command
./script5.sh
The image shows that the value of the variable i has been increased one by one using the while loop with += notation.
Case 02: Incrementing a Variable using “while” Loop with “+1” Notation
You can increment a variable using the while loop with +1 notation. Here, I will accomplish this by developing a Bash script. To know more, follow the below script.
Script (script6.sh) >
#!/bin/bash
i=0
while [ $i -lt 5 ]
do
echo $i
let “i=i+1”
done
The i=0 command assigned the value of i equal to zero. Then the while [ $i -lt 5 ] command initiates the while loop and checks whether i is less than 5, and if the condition is true it passes a zero status and thus executes the echo $i command. Then, the let “i+=1” command increments the value of i by one. Finally, done denotes the end of the while loop.
Run the script by executing the following command
./script6.sh
The image shows that the value of the variable i has been increased one by one using the while loop with +1 notation.
Example 04: Increment the Variable in Bash Using Until Loop
The until loop is a different type of loop, It passes a zero return value until the condition is true. Thus, a command inside the loop is executed. Here, I have a bash script that has a until loop inside it and does an increment operation. To know more, follow the below script.
Script (script7.sh) >
#!/bin/bash
#value of the i has been assigned
i=0
#until loop will execute the echo command inside it until i value is greater than 5
until [ $i -gt 5 ]
do
#value of i will be printed on the terminal
echo $i
#value of i will be increased by one and will be saved on the i variable
i=$((i+1))
done
The i=0 command has assigned zero as a value of i. After that the until [ $i -gt 5 ] command initiates the until loop and executes the inside command until the value of i is not greater than 5. Inside the loop, the echo $i command prints the value of the i variable on the terminal. Afterwards, the i=$((i+1)) command increments the value of the i variable by one. Finally, the done command denotes the end of the until loop.
Run the script by executing the following command
./script7.sh
The image shows that integer from 0 to 5 has been printed on the terminal using the until loop with +1 operation.
Conclusion
In this article, I have shown you some techniques to pre-increment and post-increment the bash variables utilizing for loop, while loop, and until loop. These discussed cases and examples will help you to do bash variables more productively.