SoFunction
Updated on 2025-03-10

Comparison of shell strings determines whether it is a number

Binary comparison operators, compare variables or compare numbers. Pay attention to 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, such as: 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"))
> greater than (requires double brackets), such as:(("$a" > "$b"))
>= greater than or equal to (requires double brackets), such as:(("$a" >= "$b"))

String comparison

= equal to, such as: if [ "$a" = "$b" ]
== equal to, such as: if [ "$a" == "$b" ], equivalent to =
Note: The function of == behaves differently in [[]] and [], as follows:
1 [[ $a == z* ]] # If $a starts with "z" (pattern matching) then it will be true
2 [[ $a == "z*" ]] # If $a is equal to z* (character matching), then the result is true
3
4 [ $a == z* ] # File globbing and word splitting will happen
5 [ "$a" == "z*" ] # If $a is equal to z* (character matching), then the result is true
For a little explanation, File globbing is a shorthand method about files, such as "*.c" and so on.
However, file globbing is not a strict regular expression, although the structure is similar in most cases.
!= does 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: "<" needs to be escaped in the [] structure.
> Greater than, in ASCII alphabetical order. For example:
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
Note: ">" needs to be escaped in the [] structure.
For specific reference, please refer to Example 26-11 to view the example of this operator application.
-z string is "null". It means the length is 0.
-n string not "null"
Notice:
Using -n in the [] structure, you must use "" to cause variables. Use a string that is not "" to use! -z
Or it is the string itself that is not quoted with "" and put into the [] structure. Although in general
To work, but this is not safe. It is a good habit to be used to using "" to test strings.

awk '{print $2}' | grep '^[0-9.]' > res

–b Returns true when the file exists and is a block file
-c Returns true when the file exists and is a character file
-d Returns true when pathname exists and is a directory
-e Return true when the file or directory specified by pathname exists
-f Returns true when the file exists and is a regular file
-g Return to true when the file or directory specified by pathname exists and the SGID bit is set
-h Returns true when the file exists and is a symbolic link file, this option is invalid on some old systems
-k Returns true when the file or directory specified by pathname exists and the "stick" bit is set
-p Returns true when file exists and is a command pipeline
-r Returns true when the file or directory specified by pathname exists and is readable
-s Return true when file size is greater than 0
-u Returns true when the file or directory specified by pathname exists and the SUID bit is set
-w Returns true when the file or directory specified by pathname exists and is executable. A directory must be executable for its content to be accessed.
-o Returns true when the file or directory specified by pathname exists and the user specified by the valid user ID of the quilt's current process owns it.