Created SC2166 (markdown)

koalaman
2015-08-15 22:38:12 -07:00
parent b830d0b0d1
commit 7479eb071f

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.