Increment and Decrement Variables in Bash

Increment and Decrement Variables in Bash

A variable is a placeholder that is used to store any numerical or text value so that it can be used during the execution of a computer program. It is normally used in computer programming languages, but you can also use it in Bash scripts.

In this tutorial, you will learn about increment and decrement variables and different ways of incrementing and decrementing variables in Bash scripts.

In traditional programming languages, there is a proper syntax to define a variable. Also, you have to specify the well-known data type (integer, float, char, etc.) that defines the type of value the variable would contain and allocates memory for it accordingly.

For instance, an integer type variable is defined in C++ as

int var;

In contrast, defining a variable in a Bash script is very easy. You don’t have to worry about knowing or specifying the data types.

This means variables are not typed in shell scripting. You only write the name of a variable and assign a value to it using an equal sign, as shown below.

var = 10

Types of variables

You can use the following three types of variables in your shell scripts:

  1. Environment variables are used by the operating system to save some configurational settings (e.g., PATH variable). A user can change their value.
  2. Built-in variables can be used as positional arguments with Linux commands. A user can’t change their value.
  3. User-defined variables are defined and used by the user in their shell scripts.

This tutorial is about incrementing and decrementing user-defined variables.

Different uses of a variable

In scripting, we need a place to store some values so that we can use them for various purposes later. It can be used in checking a condition (if statement), in loops (such as for, while, until), to perform some calculation (e.g., in arithmetic equations), to print something or as a loop counter.

When used as a loop counter, a variable is used to count the number of loop iterations. In this case, the variable needs to be incremented or decremented so that its value reaches some final stage.

What is an increment or decrement variable?

Incrementing means adding some number to the value of a variable. Most of the time, the value of a variable is incremented by one. Similarly, decrementing means subtracting a number (usually one) from the value of a variable.

Increment or decrement variables are normally used to control a loop so that it terminates and does not execute forever. In other words, it means if you do not increment/decrement the value of the loop variable, the loop will execute infinitely. Besides loops, these variables may also be used in other situations.

Ways of incrementing and decrementing variables

There are several ways of incrementing and decrementing variables in a Bash script. Some of them are explained below.

Arithmetic + and – operator

You can use the arithmetic + operator to add some number in a variable’s value (means to increment it). Let’s see an example:

hello.sh
#! /bin/bash

x=0
((x=x+5))
echo "The value of x = $x"

In the example above, the variable x is assigned an initial value of 0, then it is incremented by adding 5 to its initial value. Finally, the value of x is printed using the echo command as shown below.

./hello.sh
Output
The value of x = 5

[powerkit_alert type=”info” dismissible=”false” multiline=”false”]
Note: An arithmetic expression is normally written in double brackets ((..)), whereas the $ sign is used to access the value contained in a variable.
[/powerkit_alert]

Similarly, you can use the arithmetic - operator to decrement a variable’s value by any number. See the example given below.

hello.sh
#! /bin/bash

x=10
((x=x-3))
echo "The value of x = $x"

In this example, the variable x is assigned an initial value of 10, which is then decremented by 3 using the - operator. It means subtracting 3 from the current value of x and then storing the answer in x. As a result, the value of x would become 7, as shown below.

./hello.sh
Output
The value of x = 7

Arithmetic operators (+, –) used with assignment operator =

The arithmetic operator can be used in conjunction with the assignment operator (like this, += and -=). This is a short way of writing the arithmetic operators defined above.

This means that the above examples can be re-written in a short form in the following ways.

hello.sh
#! /bin/bash

x=0
((x+5))
echo "The value of x = $x"
hello.sh
#! /bin/bash

x=10
((x-=3))
echo "The value of x = $x"

You will get the same output as shown above because logically both these scripts are similar to the scripts shown as examples in method 1. The only difference is in writing the increment and decrement expressions.

[powerkit_alert type=”info” dismissible=”false” multiline=”false”]
Note: Originally writing expressions using += and -= operators is the C language convention but you can use it in Bash scripts as well.
[/powerkit_alert]

Increment and decrement operators (++, – –)

Similar to languages based on C++, you can use the increment and decrement operators (which are ++, --) in Bash scripts.

hello.sh
#! /bin/bash

x=0
((x++))
echo "The value of x = $x"
((x--))
echo "The new value of x = $x"

In this example, the variable x is assigned an initial value of 0. This value is incremented by 1 using the ++ operator. As a result, the output of the echo command would be x = 1. Then this value is decremented by 1 using the -- operator, and hence 0 is displayed as output as shown below.

./hello.sh
Output
The value of x = 1
The new value of x = 0

[powerkit_alert type=”info” dismissible=”false” multiline=”false”]
Note: the increment and decrement operators (++, --) can increase or decrease the value of a variable only by one. So, you cannot use them if you want to change the value by a number greater than one, as you saw in earlier examples given above.
[/powerkit_alert]

Difference between prefix and postfix operators

As its name implies prefix means the operator appears before a variable name (e.g., ++x). Therefore, the value of the variable would be incremented or decremented before it is used in any expression. Consider the following example.

hello.sh
#! /bin/bash

x=0
echo "The value of x = $((++x))"

In this example, the variable x is initialized with 0, but the prefix operator (++x) would increment its value by one before it gets printed as an output of the echo command. The output is shown below

./hello.sh
Output
The value of x = 1

In contrast, the postfix operator appears after the variable’s name (like x++). Hence its value would be incremented or decremented after the variable is used in any expression.

Let’s change the position of ++ in the above example to check the difference.

hello.sh
#! /bin/bash

x=0
echo "The value of x = $((x++))"

You don’t need to be surprised by getting the following output.

./hello.sh
Output
The value of x = 0

The question is why the value of x is not incremented even when you used the increment operator? The answer is that the value has been incremented but since this is a postfix operator the value of the variable has incremented after its use in the echo command. In other words, the existing value of x is used first, and then it is increment (hence the operator is named postfix).

[powerkit_alert type=”info” dismissible=”false” multiline=”false”]
Note: Similar to the increment operator, you can use the decrement operator in both prefix and postfix fashions.
[/powerkit_alert]

Conclusion

In this tutorial, you have learned about the increment and decrement variables. You also have seen different ways of using increment and decrement variables, along with some examples. If you have any questions, feel free to comment below.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like