From 8cbaf8490c4950b310177d17529f17dfcd12bdd7 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Fri, 16 May 2025 18:17:55 -0700 Subject: [PATCH] Created SC2333 (markdown) --- SC2333.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 SC2333.md diff --git a/SC2333.md b/SC2333.md new file mode 100644 index 0000000..d50e782 --- /dev/null +++ b/SC2333.md @@ -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! \ No newline at end of file