The relationship and difference between a double [[]] and a single [] shell script

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.


Example of left bracket use

1 #!/bin/bash

3 Read Val

5 ["x$val" = = "Xhello"]

7 echo $?

Operation Result:

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/82/B1/wKioL1dei_yxJHsJAAAfC4mFy0U326.png-wh_500x0-wm_3 -wmp_4-s_2138800590.png "title=" Shell.png "alt=" Wkiol1dei_yxjhsjaaafc4mfy0u326.png-wh_50 "/>

$? Get the exit status code.

Note: There must be a space when using the brackets, because it is a command, in comparison with double quotation marks, but also to add a character to both sides, to avoid the problem of the comparison of empty strings.

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 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.

The difference between the two:

[[]] there is higher tolerance than [], if A is empty, then [$a-eq 0] will be error, but [[$a-eq 0]] will not, so generally use [[]] or ["$a"-eq 0],[[]] support more than [], such as [[AAA =~a{3}],
[] There is another use, if you have a1-a9 nine files in the current directory, you can use a[1-9] to replace these nine files. A little attention, you can't use a[1-20] to replace A1-A20, you have to A[1-9] a1[0-9] A20.













This article is from the "Output Diamond pattern" blog, so be sure to keep this source http://10541571.blog.51cto.com/10531571/1788838

The relationship and difference between a double [[]] and a single [] shell script

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.