mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 11:19:45 +08:00
Created SC2166 (markdown)
21
SC2166.md
Normal file
21
SC2166.md
Normal file
@@ -0,0 +1,21 @@
|
||||
## Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
|
||||
|
||||
And likewise, prefer `[ p ] || [ q ]` over `[ p -o q ]`.
|
||||
|
||||
### Problematic code:
|
||||
|
||||
[ "$1" = "test" -a -z "$2" ]
|
||||
|
||||
### Correct code:
|
||||
|
||||
[ "$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.
|
||||
|
||||
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).
|
||||
|
||||
### Exceptions:
|
||||
|
||||
None.
|
Reference in New Issue
Block a user