Differences between (), [] and [] in unixshell

Source: Internet
Author: User
In unixshell, (), [] and [[] are different. 1. although they are very similar, they are conceptually different levels. & Quot; [[& quot;, is a keyword. many shells (such as ashbsh) do not support this method. Ksh, bash (it is said that [[support) is introduced from 2.02. & Quot; [& quot; yes... in unix shell, (), [] and [[] are different. 1. first of all, although very similar, the two are conceptually different levels of things. "[[", Is a keyword. many shells (such as ash bsh) do not support this method. Ksh, bash (it is said that [[support) is introduced from 2.02. "[" Is a command, equivalent to test, which is supported by most shells. In most modern sh implementations, "[" and "test" are internal (builtin) commands, in other words, when "test"/"[" is executed, external commands such as/some/path/to/test are not called (if such commands exist ). 2. the [[] structure is more common than the [] structure of the Bash version. All characters between [and] will not be extended or marked or separated by files, but there will be parameter reference and command replacement. Using [[...] to test the structure can prevent many logical errors in the script better than using. For example, &, |, <和> The operator can pass a [[] test, but an error occurs in the [] structure. 3. () structure extension and calculation of the value of an arithmetic expression. If the expression value is 0, 1 or false is returned as the exit status code. A non-zero expression returns a 0 or a real exit code. This structure is the opposite of the previous test command and [] structure. 4. [...] is a shell command, so the expression in it should be its command line parameter, so the string comparison operators ">" and "<" must be escaped, otherwise, it becomes the IO conversion operator (see the example in 2 above ). In [[, "<" and ">" do not need to be escaped. because "[" is a keyword and does not support command line extension, the relative syntax is slightly stricter. For example, in [...] you can use quotation marks to enclose operators, because these quotation marks are removed when you perform command line extension, and in [[...] this is not allowed. 5. [[...] while [...] 1) you can use let, () for the test number operation in ksh. the variable $ is not required for the operation. the operators are +,-, *,/, and %, we do not recommend that you use () to compare expr numbers. the operators >,>=, <=, =, and ,! = Arithmetic extensions can be used, for example: (99 + 1 <= 101) comparison of character expressions uses [[], and its operators are = ,! =,-N,-z file expression test using [[], the test uses operators-r,-l,-w,-x,-f,-d,-s,-nt, and-ot logical expressions [[], its operator!, &, | Numeric comparison, character comparison, and logical testing can be combined, for example, $ [[""! = "B" & 4-gt 3] supports wildcard extensions in bash, such as [[hest = h ?? T], [hest = h * t] when using (), no spaces are required to separate values and operators, when [[] is used, separate values and operators with spaces. 2) the [[] symbol can also be used in bash of redhat9 different from [[] in ksh. However, we recommend that you strictly follow the above principles. In bash, it is best to use () for numbers. although [[] can be used, however, if you use operators>, >=, <, <=, =, and ,! =, The result is often incorrect, however, if you use the [] operator "-eq,-ne,-le,-lt,-gt, and-ge" in [[], no error has been found. Therefore, such as $ [""! = "B" & 4> 3] such combinations (see above) cannot be used in bash. the error rate is very high. Example: [[""! = "B" & 10> 2] The result is not normal. For example, [2 \ <10] and [2 <10] are not used. It is best to use arithmetic extensions (99 + 1 = 100) instead of [[99 + 1-eq 100].
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.