Shell code that compares two strings for equality _linux shell

Source: Internet
Author: User
To compare two strings for equality:
If ["$test" x = "Test" x]; Then

Here are a few key points:
1 using a single equal sign
2 Notice that there is a space on each side of the equal sign: This is what the Unix shell requires
3 Notice the last X of "$test" X, which is deliberately arranged, because when the $test is empty, the expression above becomes x = Testx, which is obviously not equal. Without this x, the expression would have an error: [: =: unary operator expected

Binary comparison operator, comparing variables or comparing numbers. Note the difference between numbers and strings.
Integer comparisons
-eq equals, such as: if ["$a"-eq "$b"]
-ne is not equal to, 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, such as: if ["$a"-le "$b"]
Greater than (requires double brackets), such as: (("$a" > "$b")
>= is greater than or equal (requires double parenthesis), such as: (("$a" >= "$b")
Small data comparisons can use awk
String comparisons
= equals, such as: if ["$a" = "$b"]
= = equals, such as: if ["$a" = = "$b"], and = equivalent
Note: the function of = = = is different in [[]] and [], as follows:
1 [[$a = = z*]] # If $a starts with "Z" (pattern match) then it will be true
2 [[$a = = "z*"]] # if $a equals z* (character match), then the result is true
3
4 [$a = = z*] # File globbing and word splitting will occur
5 ["$a" = "z*"] # if $a equals z* (character match), then the result is true
A little explanation, about the file globbing is a shorthand for documents, such as "*.c" is, and so on.
But file globbing is not a strict regular expression, although in most cases the structure is more like.
!= is not equal to, such as: if ["$a"!= "$b"]
This operator will use pattern matching in the [[]] structure.
Greater than, in ASCII alphabetical order.
if [["$a" > "$b"]]
If ["$a" \> "$b"]
Note: In the [] structure, ">" needs to be escaped.
Refer to Example 26-11 to see examples of this operator application.
The-Z string is "null". That is, the length is 0.
-N string is not "null"

Attention:
Using-n tests in the [] structure must be caused by the variable "". Use a string that is not ""! -Z
Or the string itself, which is not quoted, is placed in the [] structure. Although in general you can
To work, but it's not safe. It is a good habit to use "" to test strings.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.