Use Let, (()), or [] for basic integer operations, using expr and BC for advanced operations, including decimal operations.
[Email protected] tmp]# str= [[email protected] tmp]# let str=str+6[email protected] tmp]# let str-=5echo $str
11
You can also self-increment, subtract from the operation.
[[email protected] tmp]# let str++; Echo $str
12
[[email protected] tmp]# let ++str; Echo
13
[[email protected] tmp]# let str--; Echo
12
[[email protected] tmp]# let--str; Echo
11
You can use $ (()) If you want the operation to be in the command, i.e. no additional assignment behavior is required.
Echo $ ((str++))
12
Echo $ ((str--)) #从这个结果中是否发现了问题?
13
Echo $ ((str--))
12
Echo $ ((str++)) #还有这个结果
11
Echo $ ((str++))
12
I do not know whether it is my calculation, or the question of $ (()), if it is $ (()) suffix-plus (i++) and suffix-decrement (i--), then after the suffix self-increment, then the suffix will again perform a self-increment behavior, in contrast, the self-increment is also.
However, using $ (()) to calculate prefix self-increment and self-subtraction transformations is no problem, and let self-decrement is no problem.
Echo $ ((--STR))
12
Echo $ ((--STR))
11
Echo $ ((++str)) #结果正常
12
Echo $ ((++STR))
13
Echo $ ((--STR))
12
You can also use [].
echo $[str= $str +6] #在 [] Use the variable symbol $
18
Echo $[str=str+6] #[] does not use the $ symbol
24
However, if $[$str =str+6] or $[$str = $str +6] An error will occur.
echo $[$str =str+6]
-bash: 24=str+6: Attempted assignment to Non-variable (Error token is "=str+6")
echo $[$str = $str +6]
-bash: 24=24+6: Attempted assignment to Non-variable (Error token is "=24+6")
Summarize the operation method of the variable:
[Email protected] tmp]# i= [[email protected] tmp]# Let I=i-1[[email protected] tmp]# Let i-=1[[email protected] tmp]# I=$ ((-1)) [[email protected] tmp]# I1 ] #中括号隔壁必须写空格 [[email protected] tmp]# i1 ]
Shell Script Raiders (learning notes)--1.5 to perform mathematical operations