The number and string comparison operation commands under the shell

  

binary comparison operators, compare variables or compare numbers. Note the difference between numbers and strings.

Integer comparison

-eq equals Such as: if [ "$a" -eq "$b" ]-ne does not equal, such as: if [ "$a" -ne "$b" ]-gt is greater than, such as: if [ " $a" -gt "$b" ]-ge is greater than or equal to, such as: if [ "$a" -ge "$b" ]-lt is less than, eg: if [ "$a" -lt " $b" ]-le is less than or equal to, such as: if [ "$a" -le "$b" ]< less than (requires double brackets), such as: (("$a" < "$b" ;)) < = less than or equal to (requires double brackets), such as: (("$a" <= "$b"))> is greater than (requires double brackets), such as: (("$a" ; > "$b"))> = greater than or equal to (requires double brackets), such as: (("$a" >= "$b"))

string comparison= Equivalent to, for example: if [ "$a" = "$b" ]== is equal to, such as: if [ "$a" == "$b" ], with = equivalent Note: == function The behavior in [[]] and [] is not , as follows: 1 [[ $a == z* ]] # If $a starts with "z" (pattern matching) then it will be true2 [[ $a == "z*" ]] # 如果$ a is equal to z* (character match), then the result is true3 [ $a == z* ] # File globbing and word splitting will occur 4 [ "$a" == "z*" ] # 如果$a Equal to z* (character match), then the result is true. The file globbing is a shorthand method for files, such as "*.c" is, like ~ is also.

but file globbing Not a strict regular expression, although in most cases the structure is more like.

!= Not equal, such as: if [ "$a" != "$b" ] This operator will Use pattern matching in the [[]] structure.

< Less than, in ASCII alphabetical order. For example: if [[ "$a" < "$b" ]]if [ " $a" /< "$b" ] Note: In the [] structure, "<" needs to be escaped.

> is greater than, in ASCII alphabetical order. For example: if [ ,null,null,3],[ "$a" > "$b" ]]if [ "$a" /> "$b" ]Note: ">& in the [] structure Quot; needs to be escaped. See Example 26-11 for an example of this operator application.

-z The string is "null". The length is 0.

- n string is not "null" Note: Testing with -n in the [] structure must be caused by "". Use a string that is not ""! -z or just The string itself referenced by "" is placed in the [] structure. Although it works in general, it is not safe. It is a good practice to use "" to test strings.

Copyright © Windows knowledge All Rights Reserved