mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2333 (markdown)
34
SC2333.md
Normal file
34
SC2333.md
Normal file
@@ -0,0 +1,34 @@
|
||||
## You probably wanted || here, otherwise it's always false.
|
||||
|
||||
### Problematic code:
|
||||
|
||||
```sh
|
||||
if [[ $answer == yes && $answer = auto ]]
|
||||
then
|
||||
echo "Feature Enabled"
|
||||
fi
|
||||
```
|
||||
|
||||
### Correct code:
|
||||
|
||||
```sh
|
||||
if [[ $answer == yes || $answer = auto ]]
|
||||
then
|
||||
echo "Feature Enabled"
|
||||
fi
|
||||
```
|
||||
### Rationale:
|
||||
|
||||
The problematic condition will never trigger because of a logic error.
|
||||
|
||||
In English one think say "I want to check for the answers `yes` and `auto`", but the test ends up checking that the answer is both "yes" and "auto" at the same time, which it can't be (if the answer is "yes", it's necessarily not "auto", and vice versa).
|
||||
|
||||
The correct check is "if the answer is `yes` or if the answer is `auto`", which will be true if the user enters either "yes" or "auto".
|
||||
|
||||
### Exceptions:
|
||||
|
||||
None
|
||||
|
||||
### Related resources:
|
||||
|
||||
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user