diff --git a/SC2166.md b/SC2166.md new file mode 100644 index 0000000..34466c4 --- /dev/null +++ b/SC2166.md @@ -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. \ No newline at end of file