What is the need for single brackets and frost brackets in shell condition judgments

Source: Internet
Author: User

1. Single brackets []

①bash's internal command, [and test is equivalent. If we don't specify the absolute path, we usually use Bash's own commands. The left bracket in the if/test structure is the command ID that calls test, and the right bracket is judged by the close condition. This command takes its arguments as a comparison expression or as a file test, and returns an exit status code based on the results of the comparison. The if/test structure does not have to be in the right bracket, but this is required in the new bash.

The comparison operators available in ②test and [] are only = = and! =, both are used for string comparisons and are not available for integer comparisons, and integer comparisons can only be used in the form of-EQ,-GT. The greater than sign is not supported either for string comparisons or for integer comparisons. If you really want to use, you can use the escape form for string comparisons, if you compare "AB" and "BC": [AB \< BC], the result is true, that is, the return status is 0. Logic and logic in [] or using-A and-o are represented.

③ the character range. Used as part of a regular expression to describe a matching range of characters. The regular is not used within brackets as a test purpose.

④ in the context of an array structure, brackets are used to refer to the number of each element in the array.

2. Double brackets [[]]

①[[is a key word for the Bash programming language. is not a command, [[]] structure is more general than [] structure. There is no filename extension or word splitting between all characters in [[and]], but parameter extensions and command substitution occur.

② supports pattern matching of strings, and even supports the shell's regular expressions when using the =~ operator. String comparisons can be made to the right as a pattern, not just a string, such as [[Hello = = Hell]], the result is true. Matches a string or wildcard character in [[]] without the need for quotation marks.

③ using [[...]] The conditional judgment structure, rather than [...], can prevent many logic errors in the script. For example,,&&, | |, <, and > operators can normally exist in the [[]] conditional judgment structure, but if they appear in the [] structure, an error will be found. For example, you can directly use if [[$a! = 1 && $a! = 2]], if you do not apply double brackets, if [$a-ne 1] && [$a! = 2] or if [$a-ne 1-a $a! = 2].

④bash the expression in double brackets as a separate element and returns an exit status code.


What is the need for single brackets and frost brackets in shell condition judgments

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.