Logical operations in shell programming, with or without, short-circuit operations, XOR operations, we use the simplest way to understand the XOR.
XOR: ^
XOR two values, same as false, different for true
Understandably, two values refer to binary values, two 1 or two 0 results are false [0], and two different values result in [1].
For example:
decimal |
| Binary
10 |
01010 |
22 |
10110 |
XOR result 28 |
11100 |
How does that differ or how does it reflect value in shell programming? The following experiment can be used in temporary variables.
#利用临时变量将a b进行互换值[[email protected] ~]#a=6[[email protected] ~]#b=8[[email protected] ~]#tmp=$a[[email protected] ~]#a=$b[[email protected] ~]#b=$tmp[[email protected] ~]#echo $a $b8 6[[email protected] ~]#b=8[[email protected] ~]#a=6[[email protected] ~]#a=$[a^b]#此时的a^b按照二进制运算,则结果为如下计算结果0110 61000 81110 14[[email protected] ~]#echo $a $b14 8[[email protected] ~]#b=$[a^b]#此时的a^b按照二进制运算,则结果为如下计算结果1110 141000 80110 6[[email protected] ~]#echo $a $b14 6[[email protected] ~]#a=$[a^b]#此时的a^b按照二进制运算,则结果为如下计算结果1110 140110 61000 8[[email protected] ~]#echo $a $b8 6
On the understanding and experiment of logical operation XOR in Shell programming