Created SC2335 (markdown)

Eisuke Kawashima
2025-07-31 17:51:39 +09:00
parent 43393d220e
commit bfa32b321e

39
SC2335.md Normal file

@@ -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`