Bash commands-comparison Operators

Source: Internet
Author: User

A binary comparison operator compares two variables or quantities.
Note that integer and string comparison
Use a different set of operators.

1. Integer comparison
-EQIs equal

If ["$ A"-EQ "$ B"]

-NeIs not equal

If ["$ A"-ne "$ B"]

-GTIs greater

If ["$ A"-GT "$ B"]

-GeIs greater than or equal

If ["$ A"-ge "$ B"]

-LtIs less

If ["$ A"-lt "$ B"]

-LeIs less than or equal

If ["$ A"-Le "$ B"]

<Is less than (within double parentheses)

("$ A" <"$ B "))


<=Is less than or equal to (within double parentheses)

("$ A" <= "$ B "))


>Is greater than (within double parentheses)

("$ A"> "$ B "))


> =Is greater than or equal to (within double parentheses)

("$ A"> = "$ B "))

2. String comparison

=Is equal
If ["$ A" = "$ B"]
Note the whitespace framing the =.

If ["$ A" = "$ B"] is not equivalent to the above.

=Is equal
If ["$ A" = "$ B"]
This is a synonym for =.
The = comparison operator behaves differently within a double-brackets test
Within single brackets.
[[$ A = z *]
# True if $ A starts with an "Z" (pattern matching ).
[[$ A = "z *"] # True if $ A is equal to z * (literal matching ).
[$ A = z *]
# File globbing and word splitting take place.
["$ A" = "z *"] # True if $ A is equal to z * (literal matching ).

! =Is not equal

If ["$ "! = "$ B"]

This operator uses pattern matching within a [[...] construct.

<Is less than, in ASCII alphabetical order
If [["$ A" <"$ B"]
If ["$ A" \ <"$ B"]

Note that the "<" needs to be escaped within a [] construct.

>Is greater than, in ASCII alphabetical order
If [["$ A"> "$ B"]
If ["$ A" \> "$ B"]
Note that the ">" needs to be escaped within a [] construct.

-ZString is null, that is, has zero length
String =''
# Zero-length ("null") string variable.
If [-z "$ string"]
Then
Echo "\ $ string is null ."
Else
Echo "\ $ string is not null ."
Fi

# $ String is null.

-NString is not null.

The-n test requires that the string be quoted within the test brackets. Using
Unquoted string! -Z, or even just the unquoted string alone within test brackets
Normally works, however, this is an unsafe practice. always quote
Tested string. [37]

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.