In the Linux shell, the type of the value of a variable is a string by default and cannot be directly numerically calculated.
[root@localhost ~]# a=9
[root@localhost ~]# b=15
[root@localhost ~]# c=$a+$b
[root@localhost ~]# echo $c
9+15
In order to perform numerical operations on variables, special methods are needed.
1. declare declares variable types
Command format: declare [+/-] [option] variable name
Options:
- Set type attributes for variables. + Cancel the type attribute of the variable. - i declares variables as integers. - x declares variables as environment variables. - p Views the type of variable being declared.
Examples:
[root@localhost ~]# a=11
[root@localhost ~]# b=22
[root@localhost ~]# declare -i c=$a+$b
[root@localhost ~]# echo $c
33
[root@localhost ~]# declare -p a
declare -- a="11"
[root@localhost ~]# declare -p c
declare -i c="33"
2. expr or let numerical operation tools
Note: There should be spaces behind expr.
Examples:
[root@localhost ~]# a=11
[root@localhost ~]# b=22
[root@localhost ~]# d=$(expr $a + $b)
[root@localhost ~]# echo $d
33
Note: The left and right sides of the + sign must have spaces, otherwise they will become string splicing or error reporting.
The role of $() is to give priority to executing the commands in parentheses and to save the execution results of the commands in memory (rather than directly output).
3. ((expression) or [expression]
Explanation: There can be spaces on both sides of brackets, or spaces can be omitted.
((expression) and [expression] have the same function.
Examples:
[root@localhost ~]# a=11
[root@localhost ~]# b=22
[root@localhost ~]# e=$(( $a + $b ))
[root@localhost ~]# echo $e
33
[root@localhost ~]# f=$[$a+$b]
[root@localhost ~]# echo $f
33
[root@localhost ~]# echo $(( (3+6)*2/9 ))
2
Explanation: The blanks on the left and right sides of + can also be omitted.
Summary: There are three main methods for numerical operation in bash. The third method is recommended because it is more convenient and flexible.
4. Operators
Operators in Linux mainly include monocular operators and binary operators, and operators also have priority.
Operators are mainly as follows:
priority | operator | type | Explain |
---|---|---|---|
13 | +,- | Monocular | Monocular positivity and monocular negativity. They are the positive and negative of the monocular operator, not the addition or subtraction. |
12 | !,~ | Monocular | Logical non-bitwise reverse or complement. |
11 | *,/,% | binocular | Multiplication, division and modulus extraction. |
10 | +,- | binocular | Add and subtract. |
9 | <<,>> | binocular | Move left and right. |
8 | <,<=,>,>= | binocular | Less than, less than or equal to, greater than, greater than or equal to. |
7 | ==,!= | binocular | Equal to, not equal to. |
6 | & | binocular | In place and. |
5 | ^ | binocular | Bit-by-bit XOR. |
4 | | | binocular | In place or. |
3 | && | binocular | Logic and. |
2 | || | binocular | Logic or. |
1 | =,+=,-=,*=,/=,%=,&=,^=,|=,<<=,>>= | binocular | Assignment, operation and assignment. |
Note: For this table, the higher the value, the higher the priority. Therefore, the assignment operator has the lowest priority.
Note: Expressions in parentheses have the highest priority and will take precedence over operations. Therefore, parentheses can be used to change the priority of operators.
The higher the priority, the operation will be carried out first; the operators with the same priority will be operated in sequence.
Examples:
[root@localhost ~]# echo $(( (3+5)*6 ))
48
[root@localhost ~]# echo $(( 14%3 ))
2
[root@localhost ~]# echo $[ 1&&1 ]
1
[root@localhost ~]# echo $[ 2&&5 ]
1
[root@localhost ~]# echo $[ 2&&a ]
1
[root@localhost ~]# echo $[ a&&b ]
1
[root@localhost ~]# echo $[ a&&a0 ]
0
[root@localhost ~]# echo $[a0&&2 ]
0
[root@localhost ~]# echo $[ 0&&b ]
0
[root@localhost ~]# echo $[ 0&&0 ]
0
[root@localhost ~]# echo $[ a035&&2 ]
0
[root@localhost ~]# echo $[ a&&a30 ]
0
[root@localhost ~]# echo $[ a&&a24 ]
0
[root@localhost ~]# echo $[ a&&abc ]
0
[root@localhost ~]# echo $[ 0||0 ]
0
[root@localhost ~]# echo $[ ab || a2 ]
0
[root@localhost ~]# echo $[ 0||null ]
0
[root@localhost ~]# echo $[ 1||0 ]
1
[root@localhost ~]# echo $[ b||0 ]
1
[root@localhost ~]# echo $[ a||b ]
1
[root@localhost ~]# echo $[ 1||1 ]
1
Explain:
For logic and (&&) operations, the value of the entire expression is 1 only when the values on both sides are non-zero.
For logic or (| |) operations, the value of the entire expression is zero only when the values on both sides are zero.
In Linux, there are three main situations that represent 0:
- 0
- null
- Strings with more than one character, such as ab, abc, a2, a0, etc.