Shell [] differs from [[]]

Source: Internet
Author: User

[] and [[]] can determine whether the variable is empty, [ $a] [[$a]] [ ! $a] [[! $a]] No difference

In Linux, we can use the test command to determine if a string is empty, and the test command gives us the "-Z option" and the "-N option," which you can use to determine if the string is empty.

The "-Z option" Determines whether the specified string is empty, returns true if it is empty, returns false if not NULL, and z can be interpreted as zero

The "-N option" Determines whether the specified string is empty, returns true if it is not empty, returns False if NULL, and-n can be understood as Nozero

when you use the options "-N" or "-Z" in test to determine if a variable is empty, you must enclose the variable with double quotation marks on the outside side. the [] is the same as the tets effect, while [[]] does not use double quotes on the variable.

When you use the Test-n command to determine if the value of variable B is empty, you should return false, because the-n option of the test command indicates that the specified string is non-empty, returns True when NULL, returns FALSE, but the return value of the command "Test-n $b" in the example above is true (should be false), which is obviously incorrect

When you use "[[]]", you cannot use "-a" or "-o" to connect multiple conditions.

When using "[]", If you use "-a" or "-o" to connect multiple conditions, "-a" or "-o" must be included within "[]".

When using "[]", If you use "&&" or "| |" Connect to multiple criteria, "&&" or "| | |" Must be outside the "[]".

Shell [] differs from [[]]

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.