mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 11:19:45 +08:00
Note that the test command has a bad design which is fixed by the ksh/bash alternative.
@@ -14,12 +14,14 @@ And likewise, prefer `[ p ] || [ q ]` over `[ p -o q ]`.
|
||||
[ "$1" = "test" ] && [ -z "$2" ]
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Rationale:
|
||||
|
||||
`-a` and `-o` to mean AND and OR in a `[ .. ]` test expression is not well defined. They are obsolescent extensions in [POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html) and their behavior is almost always undefined.
|
||||
`-a` and `-o` to mean AND and OR in a `[ .. ]` test expression is not well defined. They are obsolescent extensions in [POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html) and the lack of additional quoting levels means that expressions involving these operators are often ambiguous. Values that begin with hyphens and the `!` string are the usual issues.
|
||||
|
||||
Using multiple `[ .. ]` expressions with shell AND/OR operators `&&` and `||` is well defined and therefore preferred (but note that they have equal precedence, while `-a`/`-o` is unspecified but often implemented as `-a` having higher precedence).
|
||||
Using multiple `[ .. ]` expressions with shell AND/OR operators `&&` and `||` is well defined and therefore preferred (but note that they have equal precedence, while `-a`/`-o` is unspecified but usually implemented as `-a` having higher precedence).
|
||||
|
||||
### Exceptions:
|
||||
|
||||
None.
|
||||
If the shell variant being used is ksh derived (such as the bash shell) it will have the shell builtin command `[[ ... ]]`. This has the operators `&&`, `||`, `(`, `)`, `!` which safely avoid the ambiguity by noting which arguments were quoted and requiring the operators to be unquoted (except by the `[[ ... ]]` construct itself).
|
||||
|
Reference in New Issue
Block a user