JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Increasing variable value by one in Shell programming

Author:JIYIK Last Updated:2025/04/21 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

Run a batch (.bat) file in CMD

Publish Date:2025/04/21 Views:169 Category:OPERATING SYSTEM

This article will show you how to use CMD to run a batch file.bat. There are three ways in which you can run a batch file. Let us discuss them in the following sections. Run batch (.bat) files in CMD by directly clicking on them This way yo

Running batch scripts using Task Scheduler

Publish Date:2025/04/21 Views:188 Category:OPERATING SYSTEM

This article will show you how to use Task Scheduler to run a batch file. Running batch scripts using Task Scheduler With Task Scheduler, you can automate tasks to run automatically at specific times. It only takes a few steps and you don't

Solve the error Make Command Not Found in Cygwin

Publish Date:2025/04/21 Views:75 Category:OPERATING SYSTEM

Cygwin allows Windows users to access certain Linux features and includes a large number of GNU and open source tools that are commonly found in popular Linux distributions. When using Cygwin, it is easy to encounter a command not found err

Difference between Bash Nohup and &

Publish Date:2025/04/21 Views:188 Category:OPERATING SYSTEM

This short article introduces the nohup command and the control operator to run Linux processes in the background through Bash. In addition, we will further study the key differences between nohup and . Running Linux processes in the backgr

Bash Nohup 与 & 的区别

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

这篇简短的文章介绍了通过 Bash 在后台运行 Linux 进程的 nohup 命令和 控制运算符。 此外,我们将进一步研究 nohup 和 之间的主要区别。 在后台运行 Linux 进程 Linux 提供了两种在后台运行

Getting Timestamp in Bash

Publish Date:2025/04/21 Views:147 Category:OPERATING SYSTEM

This article discusses the date Bash command used to obtain the system date/time and UNIX timestamp. Get Timestamp Using date Command in Bash The Linux terminal uses the date command to print the current date and time. The simplest version

Pretty Printing JSON in Shell Script

Publish Date:2025/04/21 Views:145 Category:OPERATING SYSTEM

JSON is a textual method for representing JavaScript object literals and arrays and scalar data. It is relatively easier to read and write, and easier for manageable software to parse and generate. JSON is commonly used to serialize structu

Find the current folder name in Bash

Publish Date:2025/04/21 Views:189 Category:OPERATING SYSTEM

Finding a directory is very easy through Bash scripting. But finding the exact directory folder name you are in right now is a bit complicated. This article will introduce three methods to find the folder name from this article directory. I

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial