-------------------------------------------------------------------------------------------------
The types of variables in the shell are all strings by default, so:
#!/bin/Bashaa=1bb=2cc= $aa +$bbEcho $cc 1+2
Number of ways to calculate:
Declare declares the type of the variable:
declare [+/-] [option] Variable name
Options:
-: Set type attribute to variable
+: Cancels the Type property of a variable
-I: Declaring a variable as an integer (int)
-x: Declaring a variable as an environment variable
-P: Displays the declared type of the specified variable
Aa=1bb=2cc= $aa +-P AA # DECLARE--aa="1" -P AA # Declare-x Aa= "1", declare has a-CC = $aa +$BB # declare cc as numeric echo $cc 3
Method 1 (DECLARE):
Declare-i cc= $aa + $bb
Method 2 (expr or let numeric arithmetic tool):
dd=$ (expr $aa + $bb) # dd value is AA and BB and the + number must have a space on both sides to be valid
Method 3:
$ (expression) or $[expression] # If you use a single parenthesis, it means that the system command is included
Operators are: Assignment operators, numeric operators, logical operators.
echo $ ((aa=1)) # 1
echo $ ((6%4)) # 6
echo $ ((1 && 0)) # 0
Link:http://www.cnblogs.com/farwish/p/4772110.html
@ Black eyed poet <www.farwish.com>
[Shell] Bash variables: numeric operations and operators