1. Conceptually
"[[" 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"/"[" does not adjust
2. Same: Both support arithmetic comparison and string comparison expressions (use may be a bit different)
(1) "-gt", "-lt" is an arithmetic comparison operator that is used to compare the size of integers.
(2) ">", "<" is a string comparison operator used to compare the size of a string, using the dictionary order, relating to the current locale.
(3). About string comparisons. [...]、[[...]] You can compare strings in the order in which they are compared in dictionary order. In terms of ASCII characters, the Code table is arranged in the smaller, such as A<b,a<a, 1<2. Again, as long as the use of "<", ">" is a string comparison, then 9 > 100 is True, because this is actually equivalent to ' 9 ' > ' 100 ', 9 in the Code table after 1, so the string "9" is greater than the string "100". As long as you figure out when it's an arithmetic comparison, when it's a string comparison, there's usually no mistake.
(4) It is recommended to use the Let, (()) command when using numerical comparisons, otherwise error prone;
2.1 "[" Usage
$ [2-lt]&&echo True&&echo False
True
$ [2-gt]&&echo true| | echo False
False
$ [2 \<]&&echo true| | echo false #you should use "\<"
False
$ [2 \>]&&echo true| | echo false #you should use "\>"
True
2.2 "[[" Usage]
$ [[2-gt]]&&echo true| | echo False
False
$ [[2-lt]]&&echo true| | echo False
True
$ [[2 <]]&&echo true| | echo False
False
$ [[2 >]]&&echo true| | echo False
True
3. Same: Supports simple pattern matching
The pattern match here is much simpler, like the extension rule of the wildcard character of the file name. Also note that patterns on the right side of the equal sign cannot be quoted, and special features that use references to turn off certain metacharacters
3.1 "[" Usage
$ [test = Test]&&echo true| | echo False #normal Compare
True
$ [test = t*t]&&echo true| | echo False #pattern match.
True
$ [test = t: T]&&echo true| | echo False #not match.
False
$ [test = t?? T]&&echo true| | echo false #note that '? ', not '. ' stands for one single character here
True
$ [test = "T?? T "]&&echo true| | echo false #alert: Don ' t quote the pattern, using references to turn off the special features
False
3.2 "[[" Usage]
$ [[test = Test]]&&echo true| | echo False #normal Compare
True
$ [[test = t*t]]&&echo true| | echo False #pattern match.
True
$ [[test = t]. T]]&&echo true| | echo False #not match.
False
$ [[test = t??] T]]&&echo true| | echo false #note that '? ', not '. ' stands for one single character here
True
$ [[test = ' t??] T "]]&&echo true| | echo false # Alert:don ' t quote the pattern, using references to turn off the special features
False
4. Different points
4.1 Logic and logic or
(1) "[": Logic and: "-a"; Logic or: "-o";
(2) "[[": Logic and: "&&"; Logic or: "| |"
$ [[1 < 2 && B > a]]&&echo true| | echo False
True
$ [[1 < 2-a b > a]]&&echo true| | echo False
Bash:syntax Error in conditional expression
Bash:syntax error near '-a '
$ [1 < 2-a b > a]&&echo true| | echo False
True
$ [1 < 2 && B > a]&&echo true| | echo false #wrong syntax
BASH: [: Missing '] '
False
$ [1 < 2 \&\& B > a]&&echo true| | echo false #aslo wrong
BASH: [: &&: binary operator expected
False
4.2 Command-line arguments
(1) [...] is 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 IO redirection;
(2) because "[[" is a keyword, does not do the command line extension, so in [["<" and ">" Do not need to escape, but 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 [[...] is not allowed to do so;
$ ["-Z" "" "]&&echo true| | echo False
True
$ [-Z] "]&&echo true| | echo False
True
$ [["-Z" "" "]]&&echo true| | echo False
Bash:conditional binary operator expected
Bash:syntax error near ' "'
$ [[-z] "]]&&echo true| | echo False
True
4.3 [[...]] Perform arithmetic expansion, while [...] Don't do
$ [[99+1-eq]]&&echo true| | echo False
True
$ [99+1-eq]&&echo true| | echo False
BASH: [: 99+1:integer expression expected
False
$ [$ (99+1))-eq]&&echo true| | echo False
True
4.4 Regular expression matches "=~"
Regular expression match. This operator is introduced with version 3 of Bash.
The =~ Regular expression matching operator within a double brackets test Expression.
Turn from: 7905818
In the shell [and [the similarities and differences