Decrement Variable Value in Bash Scripts [4+ Examples]

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.

Steps to Follow >

  1. At first, launch an Ubuntu Terminal.
  2. 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.
  3. 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.

  4. Press CTRL+O and ENTER to save the file; CTRL+X to exit.
  5. 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.
  6. Run the script by the following command:
    ./script1.sh

    The initial value of that variable, 10, is printed first, and then the value is decreased.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.

You can follow the steps mentioned in case 01 of Example 01 to know how to write, save, and make the bash script executable.

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
EXPLANATION

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

Pre-decrement has decreased the value from 10 to 9, then 9 is printed on the terminal.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.

You can follow the steps mentioned in case 01 of Example 01 to know how to write, save, and make the bash script executable.

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
EXPLANATION

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

Pre-decrement has decremented the variable's value from 5 to 1 one by one and printed every integer utilizing the for loop.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.

You can follow the steps mentioned in case 01 of Example 01 to know how to write, save, and make the bash script executable.

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
EXPLANATION

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

Post-decrement has decremented the variable's value from 5 to 1 one by one and printed every integer utilizing the for loop.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.

You can follow the steps mentioned in case 01 of Example 01 to know how to write, save, and make the bash script executable.

Script (script5.sh) >

#!/bin/bash
i=5
while (( $i > 0 ))
do
echo $i
let “i-=1”
done
EXPLANATION

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 value of the variable, i has been decreased one by one using the while loop with -= notation.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.

You can follow the steps mentioned in case 01 of Example 01 to know how to write, save, and make the bash script executable.

Script (script6.sh) >

#!/bin/bash
i=5
while [ $i -gt 0 ]
do
echo $i
let “i=i-1”
done
EXPLANATION

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 value of the i variables has been decreased one by one using the while loop with -1 notation.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.

You can follow the steps mentioned in case 01 of Example 01 to know how to write, save, and make the bash script executable.

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
EXPLANATION

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 integer decrementing from 5 to 1 has been printed on the terminal using the until loop.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

How do you decrease a variable in bash?
You can easily decrease the value of a variable by one by using the decrement operator ( — ).
When you decrement a variable, do you subtract a value from it?
Yes, the decrement operator mainly subtracts a quant from that variable; thus decrement of a Bash variable is done 
Can a variable change its type?
Yes, you can change the data type for a variable at any time by using the variable type setting in the variables tab. Existing values are converted to the new type.
What type of variable Cannot be changed?
A constant or controlled variable cannot be altered by the program during normal execution, i.e., the variable pi is constant. And its value is 3.1416.

Related Articles


<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment