While Bash offers an extensive array of functions and features, it’s often the fundamental operations that form the building blocks of more complex scripts. In this article, I will explore the straightforward way of incrementing the value of a variable by adding 1 to the variable. Whether you are a beginner eager to grasp the essential skill, join me as I demonstrate the art and science of adding 1 to a Bash variable.
Free Downloads
3 Practical Examples Related to Adding 1 to Bash Variable
In this section, I have listed 3 practical examples related to adding 1 to bash variable. I hope it will help you to build a solid foundation for adding 1 to the bash variable.
Example 01: Add 1 to a Variable Using “+1” Operator in Bash Script
Here I will show you an example where I will add 1 to a variable using the +1 operator and then I will print the initial value and final value of the variable on the terminal. 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
#setting value to i variable
i=12
#printing value of i variable
echo $i
#increasing the value of the i variable
i=$((i+1))
#printing value of i variable
echo $i
#! /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 i=12 command assigned a value to the i variable. Afterwards, the echo $i command has printed the value on the terminal. Then i=$((i+1)) command has added 1 to the initial value of i and saved it to i. And finally, the echo $i command has printed the value of i 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
Firstly, the Bash script printed the initial value of the i variable which is 12, then added 1 to that variable using +1 operator and printed 13 on the terminal.
Example 02: Add 1 to a Variable Using “+=” Operator
Here I will show you an example where I will add 1 to a variable using += operator and then I will print the initial value and final value of i on the terminal. To know more, follow the below script.
Script (script2.sh) >
#!/bin/bash
#setting value to i variable
i=10
#printing value of i variable
echo $i
#increasing the value of i variable
((i+=1))
#printing value of i variable
echo $i
the i=10 command assigned a value to the i variable. Afterward, the echo $i command has printed the value on the terminal. Then ((i+=1)) command has added 1 to the initial value of i and saved it to i. And finally, the echo $i command has printed the modified value of i on the terminal.
Run the script by executing the following command.
./script2.sh
Firstly, the Bash script printed the initial value of the i variable 10, then added 1 to that variable using +1 operator and printed 11 on the terminal.
Example 03: Add 1 to a Variable Using “for” Loop
You can add 1 to a variable repetitively utilizing the for loop. Here I will develop a script where the value of the variable i 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. After printing the value, the value of the i variable is incremented.
Run the script by executing the following command.
./script3.sh
The image shows that the variable’s value has been increased from 1 to 5 one by one and printed every integer utilizing the for loop.
Conclusion
I hope, after going through this article, you have gained some practical knowledge about adding 1 to bash variables and you will be able to implement this knowledge and skill to your bash script.
People Also Ask
How do I add 1 to a number variable in bash?
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.
How do you add variables together in bash?
Bash allows its users to concatenate strings by writing strings one after the other or joining them using the += operator.
How to add strings in bash?
You can easily add two variables together by following the command: result=$variable_1 + $variable_2
What is the advantage of bash script?
The bash script has some advantages like, it is simple to create and can run multiple commands easily. It has an Interactive debugging feature.
Related Articles
- How to Echo Variables in Bash Script? [4 Practical Examples]
- How to Use String Variables in Bash Script? [4 Cases]
- How to Append String to Bash Variable? [2 Effective Ways]
- How to Check If Bash Variable Exists? [2 Effective Methods]
- How To Check if Bash Variable is Empty? [2 Easy Methods]
- How to List and Set Bash Environment Variables? [3 Methods]
- 2 Ways to Unset Environment Variables Using Bash Script
- 5 Methods to Check If Environment Variable is Set in Bash Script
- How to Set Bash $PATH Variable? [Easiest Configuration]
- 2 Cases to Execute Command Stored in Bash Variable
- How to Store Command Output to Bash Variable? [3 Examples]
- How to Read a File into Bash Variable? [2 Simple Methods]
- How to Write Bash Variable to File? [3 Effective Methods]
- Compare Variables in Bash Scripts [3 Practical Cases]
- Decrement Variable Value in Bash Scripts [4+ Examples]
- Addition of Bash Variable [4+ Examples]
- How to Subtract Two Bash Variables? [4+ Easy Approaches]
- How to Multiply Variable in Bash [6+ Practical Examples]
- Variable Substitution in Bash [Replace Character & Substring]
<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial