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