From a01aed0b1726cb105d2554b6f21bbda6a8519330 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sun, 6 Dec 2020 21:24:55 -0800 Subject: [PATCH] Created SC1139 (markdown) --- SC1139.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 SC1139.md diff --git a/SC1139.md b/SC1139.md new file mode 100644 index 0000000..dbed380 --- /dev/null +++ b/SC1139.md @@ -0,0 +1,36 @@ +## Use || instead of '-o' between test commands. + +And variations, like "Use `&&` instead of `and`". + +### Problematic code: + +```sh +if [ "$1" = "-v" ] -o [ -n "$VERBOSE" ] +then + echo "Verbose log" +fi +``` + +### Correct code: + +```sh +if [ "$1" = "-v" ] || [ -n "$VERBOSE" ] +then + echo "Verbose log" +fi +``` +### Rationale: + +You have a `[ .. ]` or `[[ .. ]]` test expression followed by `-o`/`-a` (or by Python-style `or`/`and`). + +`-o` and `-a` work *inside* `[ .. ]`, but they do not work *between* them. The Python operators `or` and `and` are never recognized in Bash. + +To join two separate test expressions, instead use `||` for "logical OR", or `&&` for "logical AND". + +### Exceptions: + +None + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file