First, Introduction
[equivalent to test, which is the internal command of bash, the coreutils package for the Gnu/linux system usually takes/usr/bin/test and/usr/bin/[commands. If we don't specify the absolute path, we usually use Bash's own commands.
[[is the BASH keyword (which is said to be introduced from 2.02 to [[Support]
Second, similarities and differences analysis of the same point
- Arithmetic comparisons are supported ("GT", "GE", "eq", "ne", "LT", "le")
2 Ten true false true
2 Ten true false false
2 Ten true false true
2 Ten true false false
- String comparisons are supported ("<", ">" but somewhat different for specific use)
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".
2 \< Ten true false false
2 \> Ten true false true
Note the use of the escape character \, or bash will consider it a standard output redirect.
2 < Ten true false true
2 > Ten true false false
- Support for simple pattern matching
true false true
true false true
true false false
true false true
true false true
true false true
true false false
true false true
The pattern matches the extended rule of a wildcard character that resembles a 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 are used.
Different points
- Logical AND Logical OR
- [Logical AND:-A; logic or:-a]
[1-lt 2]:true; [B > A]:true
[email protected]:~$ [1 -lt 2 -A B > a]&&echo true | | echo false true
[email protected]:~$ [1 -gt 2 -a b > a]& &echo true | | echo false false
[email protected]:~$ [1 -gt 2 -O B > A]& Amp;&echo true | | echo false true
- [[Logical AND:&&; logic or: | |]]
[1-lt 2]:true; [B > A]:true
[email protected]:~$ [[1 -lt 2 && B > A]]&&echo true | | echo false true
[email protected]:~$ [[1 -gt 2 && B > A ]]&&echo true | | echo false false
[email protected]:~$ [[1 -gt 2 | | b > A]] &&echo true | | echo false true
- Command-line arguments
"[[" is a keyword that does not do a command-line extension, so in [[Middle < "and" > "does not need to be escaped, 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 [[...] It is not allowed to do so
"-Z" "" true false true
"-Z" "" true false operator expected-bash:syntax error near ' "'
- Arithmetic extension
[[ ... ]] Perform arithmetic expansion, while [...] No, [...] Need to use $ (())
+1truefalse+1: integer expression Expectedfalse
$ ((99+1)) - true false true
+1truefalsetrue
The basics of Shell programming [and the similarities and differences