From 8b10bf75e571c18e40ddcc81df8d8818e0effefb Mon Sep 17 00:00:00 2001 From: koalaman Date: Fri, 19 Jan 2018 19:11:11 -0800 Subject: [PATCH] Created SC1026 (markdown) --- SC1026.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 SC1026.md diff --git a/SC1026.md b/SC1026.md new file mode 100644 index 0000000..39575ab --- /dev/null +++ b/SC1026.md @@ -0,0 +1,30 @@ +## If grouping expressions inside [[..]], use ( .. ). + +### Problematic code: + +```sh +[[ [ a || b ] && c ]] +[ [ a -o b ] -a c ]] +``` + +### Correct code: + +```sh +[[ ( a || b ) && c ]] +[ \( a -o b \) -a c ]] # or { [ a ] || [ b ]; } && [ c ] +``` +### Rationale: + +`[ .. ]` should not be used to group subexpressions inside `[[ .. ]]` or `[ .. ]` statements. + +For `[[ .. ]]`, use regular parentheses. + +For `[ .. ]`, either use escaped parentheses, or preferably rewrite the expression into multiple `[ .. ]` joined with `&&`, `||` and `{ ..; }` groups. + +### Exceptions: + +None + +### Related resources: + +* [Bash Pitfalls: `if [ [ a = b ] && [ c = d ] ]; then ...`](https://mywiki.wooledge.org/BashPitfalls#pf11) \ No newline at end of file