FUNDAMENTALS A Complete Guide for Beginners
The decrement operation on the bash variable is the tool for repetitive operations for a specific time. Basically, decrement means you can decrease the value of the operand by one, two, three, or whatever you want. It is a handy tool for solving complex problems and automating repetitive tasks. In this article, I will explore some examples related to the decrementing bash variable. So, without further delay, let’s dive deep into decrement.
Key Takeaways
- Learning about implementing for loop, while loop, and while loop for decrementing bash variable.
- Knowing about the post-decrement and pre-decrement operations on Bash variables.
Free Downloads
Decrement in Bash
The decrement is mainly the operation of decreasing the value of a variable. It is an important tool for bash programmers. You can decrease the variable value by one, two, three, four, etc., according to your necessity.
Types
- Post-decrement: In post-decrement, at first, the variable’s value is returned, and then the value is decreased.
- Pre-decrement: In pre-decrement, at first, the variable’s value is decreased, and then the value is returned.
Syntax
Syntax for pre-decrement is
--variable
Syntax for post-decrement is
variable--
Operators
There are some operators to decrement the bash variable. Some operators are given below.
Operator | Function |
---|---|
– | It is a basic operator for decrement. An example of this operator is variable = variable – 1. |
— | An example of this operator is the –variable. |
-= | An example of this operator is variable-=1. |
4 Practical Examples Related to the Decrement of Variables in Bash Script
The operation of decrementing bash variables is a necessary operation for looping, counting, progress tracking, and conditional operations. These operations are necessary for data manipulation and automation of complex tasks.
Example 01: Decrement the Variable in Bash
You can do post-decrement by using the x– operator and pre-decrement by using the –x operator. In two cases, I will now show both briefly.
Case 01: Using the “x–” Syntax for Post Decrement the Variable in Bash
In post-decrement operation, decrement 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 decremented. 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
EXPLANATION- 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 decrement of the variable a a=$((x- -)); #printing the value of a echo $a
EXPLANATION#! /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-decremented 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
EXPLANATION- 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 the initial value of that variable, 10, is printed first, and then the value is decreased.
Case 02: Using “–x” Syntax for Pre Decrement the Variable in Bash
In pre-decrement operation, decrement operation can be done before the main task. Here I will develop a script where the value of variable x will be decremented, and then the decremented 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 decrement of the variable a
a=$((--x));
#printing the value of a
echo $a
The x=10 command assigned a value to the x variable. Afterwards, the a=$((–x)); command has pre-decremented the x variable and assigned it to the a variable. Finally, the echo $a command has printed the variable’s value on the terminal.
Run the script by executing the following command
./script2.sh
The image shows that pre-decrement has decreased the value from 10 to 9, then 9 is printed on the terminal.
Example 02: Decrement Variable Value With the for Loop
In this section, I will show you the decrementing variables using the for loop. In the first case, I will show you pre-decrement, and in the second case, I will show you post-decrement.
Case 01: Using “for” Loop to Perform Pre-Decrement of a Variable
In pre-decrement operation, decrement operation can be done before the main task utilizing the for loop. Here, I will develop a script where the value of variable x will be decremented, and then the decremented value will be assigned to variable a. To know more, follow the below script.
Script (script3.sh) >
#!/bin/bash
#initializing for loop
for ((i=5; i>0; - -i)); do
#printing the value of the variable i
echo $i
done
The for ((i=5; i>0; – -i); do command initializes the for loop with the value of i variable 5 and decrements it by one on every iteration. After that, the echo $i command has printed every i value. After printing the value, the value of the i variable is decremented.
Run the script by executing the following command
./script3.sh
The image shows that pre-decrement has decremented the variable’s value from 5 to 1 one by one and printed every integer utilizing the for loop.
Case 02: Using “for” Loop to Perform Post-Dncrement of a Variable
In post-decrement operation, decrement 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 decremented. To know more, follow the below script.
Script (script4.sh) >
#!/bin/bash
#initializing for loop
for ((i=5; i>0; i–)); do
#printing the value of the variable i
echo $i
done
The for ((i=5; i>0; i- -); do command initialize the for loop with the value of i variable 5 and decrement 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 decremented.
Run the script by executing the following command
./script4.sh
The image shows that post-decrement has decremented the variable’s value from 5 to 1 one by one and printed every integer utilizing the for loop.
Example 03: Decrement Variable Value With the while Loop
The while loop is another approach to decrement variables. Here, I will briefly show two cases of decreasing variable values with the while loop. The first one is by using -= notation, and the second one is by using -1 notation.
Case 01: Decrementing a Variable Using “while” Loop with “-=” Notation
You can decrement 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=5
while (( $i > 0 ))
do
echo $i
let “i-=1”
done
The i=5 command assigned the value of i equal to 5. Then, the while (( $i > 0 )) command initiates the while loop and checks whether i is greater than 0, and if the condition is true, it passes a zero status and thus executes the echo $i command. Then, the let “i-=1” command decrements the i value 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 decreased one by one using the while loop with -= notation.
Case 02: Decrementing a Variable Using “while” Loop with “-1” Notation
You can decrement 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=5
while [ $i -gt 0 ]
do
echo $i
let “i=i-1”
done
The i=5 command assigned the value of i equal to 5. Then the while [ $i -gt 0 ] command initiates the while loop and checks whether i is greater than 0, and if the condition is true, it passes a zero status and thus executes the echo $i command. Then, the let “i=i-1” command decrements the i value 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 i variables has been decreased one by one using the while loop with -1 notation.
Example 04: Decrement the Variable in Bash Using “Until” Loop
Until loop is a different type of loop, it passes zero return value until the condition is true. Thus, meanwhile, a command inside the loop is executed. Here, I have a bash script that has a until loop inside it and does a decrement operation. To know more, follow the below script.
Script (script7.sh) >
#!/bin/bash
#value of the i has been assigned
i=5
#until loop will execute the echo command inside it until i value is greater than 5
until [ $i -lt 0 ]
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=5 command has assigned zero as a value of i. After that the until [ $i -lt 0 ] command initiates the until loop and executes the inside command until the value of i is not less than 0. Inside the loop, the echo $i command prints the value of the i variable on the terminal. Afterwards, the i=$((i-1)) command decrements 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 the integer decrementing from 5 to 1 has been printed on the terminal using the until loop.
Conclusion
I have shown you some practicals related to the decrement of bash variables in this article. After going through this article, you will be competent enough to decrement bash variables efficiently and automate your repetitive tasks in the blink of an eye.
People Also Ask
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]
- Increment Variable Value in Bash Scripts [4+ Examples]
- Adding 1 to Bash Variable [3 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