From 173fdb2d8b20fd1721a777420b58030ed8499d03 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Tue, 8 Apr 2025 11:13:35 -0700 Subject: [PATCH] Created SC2332 (markdown) --- SC2332.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 SC2332.md diff --git a/SC2332.md b/SC2332.md new file mode 100644 index 0000000..d15393f --- /dev/null +++ b/SC2332.md @@ -0,0 +1,47 @@ +## [ ! -o opt ] is always true because -o becomes logical OR. Use [[ ]] or ! [ -o opt ]. + +Or "`[ ! -a file ]` is always true because `-a` becomes logical AND. Use `-e` instead." + + +### Problematic code: + +```sh +if [ ! -o braceexpand ] +then + .. +fi +``` + +### Correct code: + +```sh +if [[ ! -o braceexpand ]] +then + .. +fi +``` + +or + +```sh +if ! [ -o braceexpand ] +then + .. +fi +``` + +### Rationale: + +Bash interprets `[ ! -o opt ]` as `[ "!" ] || [ "opt" ]` instead of negating the condition. As a result, the condition is always true. + +Avoid this by using `[[ ! -o opt ]]` or `! [ -o opt ]`. + +The same issue applies to `[ ! -a file ]`, but this is easier fixed using POSIX standard `[ ! -e file ]`. + +### Exceptions: + +None. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file