JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Increasing variable value in Shell programming

Author:JIYIK Last Updated:2025/03/23 Views:

In this article, we will explain how to increment a variable in bash. We will also learn about different types of increment operators used in bash scripting in Linux.

Let's start with shell programming in Linux and learn how to increment a variable in bash.


Increment a variable in Bash using Linux

Linux Bash Shell is a full-featured shell with programming capabilities. Variables and arithmetic operations such as addition, division, subtraction, multiplication, etc. are provided in bash.

This incrementing process can be performed in multiple ways in bash shell. So, let’s explore them below.


Types of increment operators used in Bash

There are many ways to increment the value of a variable in bash programming, including

    • Operators
  • ++ Operator
  • += Operator
  • Loop

Let's discuss each of the above methods with code examples.

Use the + operator to increment the value of a variable by 1

In Bash programming, we use +the operator with $ sign to increment. This is the simplest way to increase a variable.

Using +the operator, we can increase a value by 1. Let's try this with an example:

Sample code:

# declear a variable ad assign it value 0
i=0
# print it to check the value of the variable
echo $i

The output of the above code is:

0

Now we will use the + operator to increment the value by 1.

# use the (``+` and `$` ) sign to increment the value of the variable by one
i=$((i+1))
# print the variable to check the new value
echo $i

Output after increment:

1

Use the ++ operator to increment the value of a variable by 1

The most practical way to increment a bash variable with a single statement is to use ++the operator. No need to specify the increment value and other details.

Variables and ++operators can be used directly together. There are two ++ways to use the operator.

  • prefix - ++iThe version is called a prefix and the variable value is incremented by one before use.
  • Postfix - i++The version is called postfix, and the variable value is incremented by 1 after use.

Let's look at an example to get a better grasp of prefix and postfix operators. Starting with the prefix increment operator, declare a variable and assign a value to it.

i=2

Now, we will use ++the operator to increment and print a variable in one line of code.

Example code using ++the operator:

echo $((++i))

The output of the code is:

3

The following code example explains how we can use the postfix increment operator. First, declare a variable and assign a value to it.

i=12

Now increment it using postfix operator and print it.

echo $((i++))

The above code produces the following output:

12

Check out the sample code below to understand the prefix and postfix operators.

Sample code:

i=20
echo $i
echo $((i++))
echo $i
echo $((++i))
echo $i
echo $((i++))
echo $i

Output of the code:

20
20
21
22
22
22
23

Use the += operator to increment the value of a variable by 1

+=The operator is another popular operator using which we can increment bash variables. When using this operator, we write a single line of code to assign the first operand and the result variable.

First, we will create a variable and assign a value to it, then we will +=increment it using , after which we will print the variable to check the new value.

Example code using +=the operator:

i=55
((i+=1))
echo $i

Output of the code:

56

Use a for loop to repeatedly increment the value of a variable by 1

One of the most important tasks when using loops in any programming language is incrementing the value of a counter or iterator.

Doing this helps us reach the termination condition of the loop, otherwise our loop will run indefinitely.

Today, we will look at multiple techniques for incrementing variables in bash. But first, let's try out some increment examples using loops.

Example code using a for loop:

for ((j = 0 ; j < 5 ; j++ )); do echo "$j"; done

Output of the code:

0
1
2
3
4
5
6
7
8
9

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

How to use the Linux file remote copy command scp

Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM

Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial