Bash condition test-empty string confusions: bash condition string

Source: Internet
Author: User

Bash condition test-empty string confusions: bash condition string

You can use-n or-z to check whether a string is null. According to the meaning of the operator, if VAR is null or undefined, the-n test result should be false, and-z should be true. Otherwise,-n is true and-z is false.

The conditional test list is used, that is, "[-n $ VAR] & echo TRUE". If TRUE is output, the conditional test result is TRUE.

After testing, it is clear that when VAR is not empty, the test results are normal, while VAR is empty, and the test results are true, but many of the Manual guides (including ABS "Advance Bash Scripting Guide" example 7.1) Say This should work properly. Why?

Write the preceding statement into the script and execute it with bash-x. It is found that the test condition is "'['-n'] '", that is, after word segmentation, the empty variable actually disappears, and the test command becomes a parameter with only "-n". In the bash Manual (bash4.2), the test (equivalent to the '[' command, note: "[" is actually a command.) The command returns true if there is only one parameter and the parameter is not empty. That is to say, if you change "-n" to another test operator, such as "-z" or "-e", the test results are true. A try.

What is the solution?

Method 1: do not directly use the variable $ {VAR}. Instead, append an arbitrary character to test whether the variable is null. For example, "[$ {VAR} y = y] & echo TRUE" Will Output TRUE, while "[$ {VAR} y! = Y] & echo TRUE "won't be output.

Method 2: Use the "if [[conditional-expression]" format. "[" Is a keyword rather than a command. In this form, "[[...] "does not perform word segmentation, So execute the script with bash-x and find that" [-n $ VAR] "is" [-n '']", that is to say, the empty string is still saved as a parameter (I .e. ''). According to the test evaluation rule, this is two parameters, so the test operator can work normally. In this case, ABS emphasizes that it is reasonable to use the [[...] structure as much as possible.

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.