Introduction
In Bash scripting, variables are essential for storing and manipulating data. They serve as placeholders for different types of values, such as numbers, strings, arrays, etc.
Increment or decrement the values of the variables during script execution to perform various calculations and track changes in your program.
In this tutorial, you will learn to increment and decrement a variable in Bash.
Prerequisites
- A system running Linux.
- Access to the terminal.
Incrementing a Variable in Bash
Incrementing a variable means increasing its value by a certain amount, typically by adding a number or by increasing it by 1. It is often used for counting, iterating through loops, or tracking progress.
Note: See how to print a variable in Bash using printf.
Increment Bash Variable with + Operator
Most programming languages allow using the +
operator to increment a variable's value. Increment a variable by summing it with any value. By default, the $((…))
operators denote the use of the +
operator in an arithmetic operation.
Follow the steps below to create a script and view the process:
1. Open the terminal (Ctrl + Alt + T) and create a new Bash script. Use a text editor such as vi/vim:
vi script.sh
2. Enter the following code:
#!/bin/bash
i=12
echo $i
i=$((i+1))
echo $i
Here, ((i+1))
increments the i
variable by 1.
3. Save the script and exit vi:
:wq
4. Run the Bash script to test if it works:
bash increment.sh
The output shows that the i
variable value has been incremented by 1, from 12 to 13.
Increment Bash Variable with += Operator
The +=
operator in Bash also increments a variable by a specified amount. It is a shorthand notation for the sum
operator, where the first operand and the result variable name are the same and assigned in a single statement.
Follow the steps below to test the +=
operator and increment a variable:
1. Create a new Bash script using a text editor.
2. Enter the following code:
#!/bin/bash
i=10
echo $i
((i+=1))
echo $i
In this case, ((i+=1))
increments the i
variable by 1.
3. Save the script and run it:
The variable value has been incremented by 1, from 10 to 11, as seen in the output.
Increment Bash Variable with ++ Operator
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. Use the ++
operator directly with the variable.
Follow the steps below to test the ++
operator in a Bash script:
1. Create a new Bash script.
2. Paste the following code:
#!/bin/bash
i=10
echo $i
echo $((i++))
echo $i
echo $((++i))
echo $i
((i++))
echo $i
In this example:
$((i++))
increments the value ofi
by 1 but returns the previous value ofi
before the increment.echo $((i++))
prints the value ofi
before the increment.echo $i
prints the updated value ofi
after the increment.$((++i))
increments the value ofi
by 1 and returns the updated value.echo $((++i))
prints the value ofi
after the increment.
The i++
and ++i
operators increment the variable in different ways and the $(( ))
construct is used for arithmetic evaluation.
4. Save the script and run it:
The script initializes the variable i
with the value 10, increments it in two different ways, and prints the intermediate and final values.
Decrementing a Variable in Bash
Decrementing a variable means reducing its value by a certain amount, usually by subtracting a specific number, i.e., by decreasing it by 1. It is commonly used in scripting to iterate through a loop or track a decreasing count.
Decrement Using the - Operator
The simplest way to decrement a variable in Bash is to use the -
operator. Follow the steps below to decrement a variable in Bash using an until loop:
1. Create a new Bash script.
2. Paste the following code:
#!/bin/bash
count=5
until [ $count -le 0 ]
do
echo count: $count
((count=count-1))
done
In the above code:
count=5
declares a variable namedcount
and assigns it an initial value of 5.until
is a loop control structure in Bash that executes the loop as long as the condition[ $count -le 0 ]
is false. In this case, the loop continues until the value ofcount
is less than or equal to 0.((count=count-1))
decrements the value ofcount
by 1. It subtracts 1 from the current value ofcount
and assigns the result back tocount
.
3. Save and run the script:
The script decrements the variable value in the loop until the set condition is true.
Decrement Using the -= Operator
Aside from the basic - operator, Bash also supports the -=
operator. This operator decrements the value of the left operand with the value specified after the operator.
Note: See how to use bash break to exit from a loop, or bash continue to resume a loop.
Follow the steps below to test the -=
operator in a while
loop:
1. Create a new Bash script.
2. Paste the following code:
#!/bin/bash
i=20
while [ $i -ge 5 ]
do
echo Number: $i
let "i-=5"
done
In the above code:
i=20
declares a variable namedi
and assigns it an initial value of 20.while
continues executing the loop as long as the condition[ $i -ge 5 ]
is true. In this case, the loop will continue until the value ofi
is greater than or equal to 5.let "i-=5"
decrements the variable value by 5 and assigns the result back toi
. The let command is a shorthand notation for performing arithmetic operations in Bash.
3. Save and run the script:
The script decrements the variable value by 5 until the value equals 5.
Decrement Using the -- Operator
The --
operator also decrements variable values. However, it decrements the variable only by 1, unlike other operators that accept other values. You can use --
as prefix or postfix operators, which means you can add them immediately before or after the operator.
Follow the steps below to decrement a variable using --
:
1. Create a new Bash script.
2. Paste the following code:
#!/bin/bash
counter=10
((counter--))
echo "Counter: $counter"
In this example:
counter=10
declares a variable namedcounter
and assigns it an initial value of 10.((counter--))
decrements the value ofcounter
by 1 using the--
operator. It subtracts 1 from the current value ofcounter
.echo "Counter: $counter"
prints the current value ofcounter
to the console.
3. Save and run the script:
The script decrements the variable by 1 and prints the result.
Prefix and Postfix
The difference between prefix and postfix notation when incrementing or decrementing a variable in Bash is the order and timing of the operations.
- Prefix. The operator is placed before the variable. For example,
++i
or--i
. In this case, it increments/decrements the variable before evaluating or assigning the expression. - Postfix. The operator is placed after the variable. For example,
i++
ori--
. In this case, it increments or decrements the variable after evaluating or assigning the expression.
If your aim is only to increment/decrement the variable, then there is no difference if you use the prefix or postfix operator. The distinction between the two operators is relevant only when the result of the operation is utilized in subsequent operations or assigned to another variable.
The following example shows the difference between using prefix and postfix operators:
1. Create a new Bash script and paste the following code:
#!/bin/bash
counter=5
result_postfix=$((counter++))
result_prefix=$((++counter))
echo "Postfix result: $result_postfix"
echo "Prefix result: $result_prefix"
echo "Updated counter value: $counter"
In the above code:
counter=5
declares thecounter
variable and assigns it an initial value of 5.result_postfix=$((counter++))
uses the postfix operator++
to increment the value ofcounter
after assigning its current value toresult_postfix
. The result of this expression is the original value ofcounter
(5), which is then assigned toresult_postfix
.result_prefix=$((++counter))
uses the prefix operator++
to increment the value ofcounter
before assigning its current value toresult_prefix
. The result of this expression is the incremented value ofcounter
(7), which is then assigned toresult_prefix
.echo "Postfix result: $result_postfix"
prints the value ofresult_postfix
.echo "Prefix result: $result_prefix"
prints the value ofresult_prefix
.echo "Updated counter value: $counter"
prints the updated value ofcounter
.
2. Save the script and run it:
The postfix operator (counter++
) returns the current value of counter
before the increment, while the prefix operator (++counter
) increments the value before the assignment.
Conclusion
After reading this tutorial, you should know how to increment and decrement a variable in Bash. Use the supported operators and elevate your scripting capabilities when iterating through loops or counts in a script.
Next, check out our tutorial for the Bash if elif else statement, or learn to use the Bash case statement effectively.