In a shell script, the case of integer self-increment is often involved in a while or for loop, and the following is a list of possible ways
" Way One "declare-i to declare an integer variable
- [Email protected]:~# declare-i x=1
- [Email protected]:~# x+=1
- [Email protected]:~# echo $x
- 2
" mode two " using the Let command
- [EMAIL PROTECTED]:~# I=1  
- [email protected]:~# let i+=1
- [email protected]:~# echo $i
- 2
- [EMAIL PROTECTED]:~# I=1  
- [email protected]:~# let i= $i +1
- [email protected]:~# echo $i
- 2
- [EMAIL PROTECTED]:~# I=1  
- [email protected]:~# let i++
- [email protected]:~# echo $i
- 2
- [email protected]:~# i=1
- [email protected]:~# let ++i
- [email protected]:~# echo $i
- 2
" Way three " use (())
- [Email protected]:~# i=1
- [Email protected]:~# ((++i))
- [Email protected]:~# echo $i
- 2
- [Email protected]:~# i=1
- [Email protected]:~# ((i++))
- [Email protected]:~# echo $i
- 2
" mode four " uses the expr command
- [Email protected]:~# i=1
- [Email protected]:~# i= ' expr $i + 1 '
- [Email protected]:~# echo $i
- 2
- [Email protected]:~# i=1
- [Email protected]:~# i=$ (expr $i + 1)
- [Email protected]:~# echo $i
- 2
" mode five " using $ (())
- [Email protected]:~# i=1
- [Email protected]:~# i=$ (($i + 1))
- [Email protected]:~# echo $i
- 2
" mode six " using $[]
- [Email protected]:~# i=1
- [Email protected]:~# i=$[$i + 1]
- [Email protected]:~# echo $i
- 2
Note:
1) It is better to use i=$ (expr $i + 1) than i= ' expr $i + 1 '
2) use (()) or $ (()) faster than expr
3) If the speed problem is not considered, it involves the compatibility of different platforms, it is best to use expr
4) Use more cases on Bash (SH): let,expr, (())
This article for the network collection, if there is infringement, please inform!!!
Shell Programming Learning notes-integer self-increment