Shell script----[] and [[]] the difference and contact

Source: Internet
Author: User
Tags logical operators

a , [] single and double brackets

Basic elements:

[] Two symbols should be separated by a space around

There is a space between the internal operator and the action variable: such as ["a" = "B"]

String comparisons in,> < need to be written \> \< escaped

[] in the string or ${} variables as far as possible using "" "double quotation marks, to avoid the value does not define a reference and a good way to error

[] can use –a–o for logical operation

[] is a bash built-in command: [is a shell builtin

[] is actually shorthand for the test command in bash. That is, all [expr] equals test expr.

two , [[]] both brackets

Basic elements:

[[]] Two symbols should be separated by a space

There is a space between the internal operator and the action variable: [["A" = "B"]]

in string comparisons, you can use the > < directly without escaping

[[]] the string or ${} variable as far as possible if not used "" double quotation marks, pattern and meta-character matching are performed , for example: if * is not enclosed by double quotes, all characters can be matched

For example:

[[Email protected] lxjwork]$ [["AB" =a*]] && echo "OK"

Ok

[[ ]] internal can use && | | For logical Operation

[[]] is the bash keyword:[[is a shell keyword

[[]] other usages are the same as []

[[]] and [] both can and! Use together

Priority level

! > && > | |

Logical operators < relational operators

Logical operator:! && | | -a-o

Relational operators: < > \> \< = =!=–eq–ne-gt-ge–lt-le

Third, Comparison

The [[]] operator is simply an extension of the [] operator. [[]] can support the <,> symbol operation does not require an escape character, [] must be escaped:\> \<.

[[]] is the size of the string comparison.  Inside support logical operator:&& | | ,-a-o is no longer used.

For the test command, use-eq to make a numeric comparison, and when you pass in the string, you get an error.

[[expr]] is a real conditional judgment statement in Bash whose syntax is more in line with programming habits (such as &&, | | use), in [[]] deliberately passed the string to-eq will not error, bash implementation directly converts a non-integer string directly to 0 (you can self-verify, in [[]], any need for an integer, but the supply is another can not be converted to an integer string, has become 0). This should be the result of a bash implementation that does not check for content on either side of the-EQ operator in [[]]. For us, there is no need to drill these things, and we should make sure that we always provide integers when we need to use integers in our code.

Iv. [[]] advantages over []

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

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

use [[...]] The conditional judgment structure, rather than [...], can prevent many logic errors in the script. For example, &&, | |, <, and > operators can normally exist in [[]] conditional judgment structures, but if they appear in the [] structure, they will get an error.


Shell script----[] and [[]] the difference and contact

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.