Shell Scripting Learning Notes-string test expressions

Source: Internet
Author: User
1.1 String test operator

The string test operator has the function of comparing whether two strings are the same, whether the length of a string is zero, whether the string is null (NOTE: Bash distinguishes between 0-length strings and empty strings, etc.)
The following table is a common string operator

You can also view the string test operator with the man test

String test operator Hints:

(1)-N compares whether the string length is nonzero, or true if nonzero: [-n ' $myvar]

(2)-Z compares whether string length equals zero, if equal to zero is true, such as: [-Z "$myvar"]
Special Note: Test the operation symbol for the string in the table above. For example: [-N ' $myvar "] to enclose the string in" "double quotation marks.

Precautions:

(1) string or string variable comparisons are followed by double quotation marks before being compared.

(2) string or string variable comparison, the comparison symbol at both ends of the best have spaces, learning shell Multi-reference system script.

"=" compares two strings in the same way, equivalent to "= =", such as ["$a" = "$b"] where a variable of $ A is best used "", because if there are spaces in the middle, the sign may go wrong, but the better way is ["${a}" = "${b}"].

Example Demo:

Error case with no spaces at both ends of the equals sign:

System Script Case:

Shell Scripting Learning Notes-string test expressions

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.