How do you add 1 to a variable in a shell script?
The way to add 1 to an integer-type variable in a shell script is as follows:
#! /bin/sh
#This script tests several ways to increment an integer-type variable in a shell script Add 1
#Define an integer-type variable
a=1
echo $a
#The first way to increment an integer-type variable
a=$(($a+1)) <
echo $a
#The second way to increment an integer variable
a=$[$a+1]
echo $a
#The third way to increment an integer variable
a=`expr $a + 1`
echo $a
< p>
#The fourth way to increment an integer variable
let a++
echo $a
#The fifth way to increment an integer variable
let a+=1
echo $a
#The sixth way to increment an integer variable
#The third way to increment an integer variable
let a++
echo $a
#The third way to increment an integer variable
((a++))echo $a
Shell scripts are similar to batch processing under Windows/Dos, that is, they use all kinds of commands pre-packaged into a file to facilitate the one-time execution of a program file, mainly to facilitate the administrator’s setup or management with. But it is more powerful than batch processing under Windows, and more efficient than programs edited with other programming programs, which use commands under Linux/Unix.
Variable self-increment in shell
When writing loops in Linux-shell, you often have to use variable self-increment, so now I’ll summarize the methods for integer variable self-increment.
As far as I know, in bash, there are currently five methods:
Alternatively, for a fixed number of loops, which can be realized by the seq command, there is no need for variable self-incrementation; examples are as follows:
Sun system shell script, how to complete the self-increment operation
shell self-increment
((i++))
leti=i+1
How to set self-incrementing variables in shell
loop=$(($loop+1))echo”$i “echo “num:$loop “done Note: loop=`expr$loop+1`, there can’t be a space to the left or right of the loop variable and it’s not in single quotes; in addition, for a fixed number of loops, it can be achieved by the seq command to achieve, there is no need for variable increment; examples are as follows: