Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does shell compare whether two strings are equal?

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how shell compares whether two strings are equal". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The way to compare whether two strings are equal is:

If ["$test" x = "test" x]; then

The key points here are as follows:

1 use a single equal sign

2 notice that there is a space on each side of the equal sign: this is the requirement of unix shell

3 notice the last x of "$test" x, which is deliberately arranged, because when $test is empty, the above expression becomes x = testx, which is obviously not equal. Without this x, the expression will report an error: [: =: unary operator expected

Binary comparison operator, comparison variable or comparison number. Pay attention to the difference between numbers and strings.

Integer comparison

-eq equals, for example: if ["$a"-eq "$b"]

-ne is not equal to, for example: if ["$a"-ne "$b"]

-gt is greater than, for example: if ["$a"-gt "$b"]

-ge is greater than or equal to, such as: if ["$a"-ge "$b"]

-lt is less than, for example: if ["$a"-lt "$b"]

-le is less than or equal to, such as: if ["$a"-le "$b"]

< 小于(需要双括号),如:(("$a" < "$b")) "$b")) >

= greater than or equal (double parentheses are required), such as: (("$a" > = "$b"))

Small data can be compared with AWK.

String comparison

= equal to, for example: if ["$a" = "$b"]

= equal to, for example: if ["$a" = = "$b"], equivalent to =

Note: the function of = = behaves differently in [[]] and [], as follows:

1 [[$a = = z*]] # if $a starts with "z" (pattern match) then it will be true

2 [[$a = "z*"] # if $an equals z * (character match), then the result is true

three

4 [$a = = z*] # File globbing and word splitting will happen

5 ["$a" = "z*"] # if $an equals z * (character match), then the result is true

A little explanation, about File globbing is a kind of shorthand about files, such as "* .c" is, and so is also.

But file globbing is not a strict regular expression, although in most cases the structure is similar.

! = not equal to, for example: if ["$a"! = "$b"]

This operator will use pattern matching in the [[]] structure.

< less than, in ASCII alphabetical order. Such as:

If [["$a" < "$b"]]

If ["$a"\ < "$b"]

Note: "" needs to be escaped in the [] structure. "

Refer to Example 26-11 for an example of this operator application.

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

-n string is not "null"

Note:

Using-n to test in the [] structure must use "" to generate variables. Use a string that is not "" to use!-z

Or the string itself that is not referenced by "" is placed in the [] structure. Although under normal circumstances

To work, but it is not safe. It is a good habit to use "" to test strings.

This is the end of the content of "how shell compares whether two strings are equal". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report