diff --git a/SC2335.md b/SC2335.md new file mode 100644 index 0000000..0e423d5 --- /dev/null +++ b/SC2335.md @@ -0,0 +1,39 @@ +## Use `[ "$var" -ne 1 ]` instead of `[ ! "$var" -eq 1 ]` + +and other binary operators such as + +- `=` and `!=` (string) +- `-le` and `-gt`, and so forth (arithmetic) + +### Problematic code: + +```sh +if [ ! "$var" -eq 1 ]; then :; fi +if ! [ "$var" = foo ]; then :; fi +``` + +### Correct code: + +```sh +if [ "$var" -ne 1 ]; then :; fi +if [ "$var" != foo ]; then :; fi +``` + +### Rationale: + +Double negation of such binary oprators is unnecessary. + +### Exceptions: + +This is a stylistic issue that does not affect correctness. If you prefer the original expression, you can [[Ignore]] it with a directive or flag. + +This rule is not applied to the followings: + +- `<` and `>` (lexicographical) +- `-nt` and `-ot` (file timestamp) +- `=~` (regular expression) + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! +* [[SC2236]] and [[SC2237]] for unary operators `-n` and `-z`