Differences in Unix shell (), [] and [[]]

Source: Internet
Author: User
Tags arithmetic

Tag:http   io   os    use     file    sp    div   2014   amp   

The difference between the Unix shell (), [] and [[]]   1. First of all, although very similar, but conceptually, they are different levels of things. "[[" is a keyword, and many shells (such as ash BSH) do not support this approach. Ksh, Bash (reportedly introduced support for [[support] from 2.02 onwards. "[" is a command that is equivalent to test and is supported by most shells. In most modern SH implementations, "[" and "Test" are internal (builtin) commands, in other words, "test"/"[" is not called when/some/path/to/test such an external command (if there is such a command).   2. [[]] The structure is more generic than the bash version of []. All characters between [[and]] are not separated by a file extension or a tag, but there are parameter references and command substitutions.   with [[...]] Test structure than with [...] To prevent many logic errors in the script. For example, &&,| |,< and > Operators can be passed in a [[]] test, but errors will occur in the [] structure.   3. (()) structure expands and computes the value of an arithmetic expression. If the expression value is 0, it returns 1 or false as the exit status code. An expression of a value other than 0 returns a 0 or True as an exit status code. This structure is just the opposite of the previous Test command and [] structure.   4. [ ... ] As a shell command, so the expression in it should be its command-line argument, so the string comparison operator ">" and "<" must be escaped, otherwise it becomes an IO redirect operator (see the example above in 2). in [[<] and > do not need to escape, because "[" is the keyword, does not do the command line extension, so the relative syntax is slightly stricter. For example in [...] You can enclose the operators in quotation marks, because they are removed when you do a command-line extension, and in [[...] It is not allowed to do so.   5. [[ ... ]] Perform arithmetic expansion, while [...] Do not do   1) The operation of the test number in Ksh can use Let, (()), where the operation does not require the variable $ symbol, the operator is + 、-、 *,/,%, is not recommended to use the comparison of expr numbers (()), its operators >, >=, <, < =, = =,! = can use arithmetic extensions, such as: ((99+1 <= 101)) A comparison of character expressions using [[]], whose operator =,! =,-n,-Z file expressions are tested using [[]] , its operator-R,-L,-W,-X,-F,-D,-S,-nt,-ot logical expressions are tested using [[]], its operators!, &&, | | Numeric comparisons, character comparisons, and logic tests can be combined, such as $ [["a"! = "B" && 4-GT 3]] to support the wildcard extension in bash, such as: [[Hest = h?? T]], [hest = H*t]], when using (()), no spaces are required to separate the values and operators, and [[]] are used to separate the values and operators with a space.   2) The [[]] symbol can also be used in bash in redhat9, unlike Ksh [[]]. But it is recommended to use it strictly in accordance with the above principles. In bash, the comparison of numbers is best used (()), although [[]] is used, but if the operator >, >=, <, <=, = =,! = are used inside, the result is often wrong, but if you use the operator in [] in [[]]-eq,-ne,- Le,-lt,-GT,-ge "and so on, have not yet found fault. So a combination such as $ [["a"! = "B" && 4 > 3]] (see above) is also not available in bash, with a high error rate. Example: [["a"! = "B" && > 2]] "judgment result is not normal." such as [2 \< 10], [[2 < 10]] are not used. It is best to use arithmetic extensions ((99+1 = = 100)) instead of [[99+1-eq 100]].  

Differences in the Unix shell (), [], and [[]]

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.