mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2335 (markdown)
39
SC2335.md
Normal file
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`
|
Reference in New Issue
Block a user