In the Shell script, the brackets "[]" Determine the problem

Source: Internet
Author: User
Problem Description

In the "[]" test, the result of the judgment is always true for reasons not known.
For example:

["$choice" = = "n"-o "$choice" = = "n"] && echo-e "Oh interrupt!" && exit 0

Regardless of what the variable "choice" is, the test always outputs the contents of the following. problem Reason

Then consult the data and find out why.
when using brackets as the Shell's judgment in bash syntax, the ends of the brackets must have spaces to separate them, and each component in the brackets [] needs to have the spacebar separated.
So the correct wording should be

  ["$choice" = = "n"-o "$choice" = = "n"] && echo-e "Oh interrupt!" && exit 0

That is, "= =" on both sides to add a space symbol, this way can be judged correctly.
Similarly, in "[[exp]]" judgments, the components are separated by spaces to achieve the correct results. Extended Reading The "[]" in the single bracket [] shell is almost identical to the way test is used. The left bracket in the if/test structure is the command ID that invokes test, and the right middle bracket is judged by the closing condition. This command takes its arguments as a comparison expression or as a file test, and returns an exit status code based on the result of the comparison. The comparison operators available in Test and [] are only = = and!=, both used for string comparisons, not for integer comparisons, and integer comparisons can only be used in the form of-EQ,-GT. The greater-than sign is not supported for both string comparisons and integer comparisons. If you really want to use, for string comparisons you can use the escape form, if you compare "AB" and "BC": [AB \< BC], the result is true, that is, the return state is 0. The logic and logic in [] or the use-A and-o representations. "&&" and "| |" are not available. Constants in parentheses are best used in single or double quotes, and variables are best enclosed in double quotes. The result of "= =" and "=" is the same when the brackets "[]" is judged, as

["$choice" = = "n"-o "$choice" = = "N"]

And

["$choice" = "n"-o "$choice" = "n"]

The

results are the same. double bracket [[]] [[is a keyword in the Bash program language.] is not a command, the [[]] structure is more generic than the [] structure. All characters between [[and]] do not have file name extensions or Word splits, but parameter extensions and command substitution occur. Supports pattern matching of strings, and even support for Shell regular Expressions when using the =~ operator. String comparisons can take the right side as a pattern, not just a string, such as [[Hello = = Hell]], and the result is true. [[]] matches a string or wildcard character and does not require quotation marks. use [[...]] Conditional judgment structures, rather than [...], can prevent many logical errors in the script. For example,,&&, | |, < and > operators can normally exist in the [[]] conditional judgment structure, but if they appear in the [] structure, the error occurs. Bash considers an expression in double brackets as a separate element and returns an exit status code. references "brother Bird's Linux private dish" the brackets in the third shell (parentheses, brackets, braces)

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.