From ae5d10bc77d6a4d61d241dea0fd81d955eba4684 Mon Sep 17 00:00:00 2001 From: koalaman Date: Sat, 6 Feb 2016 22:53:44 -0800 Subject: [PATCH] Created SC1102 (markdown) --- SC1102.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 SC1102.md diff --git a/SC1102.md b/SC1102.md new file mode 100644 index 0000000..0e1bd70 --- /dev/null +++ b/SC1102.md @@ -0,0 +1,26 @@ +## Shells differ in parsing ambiguous $(((. Use spaces: $( (( . + +### Problematic code: + +```sh +echo "$((( n > 0)) && mycommand --flags)" +``` + +### Correct code: + +```sh +echo "$( (( n > 0)) && mycommand --flags)" +``` +### Rationale: + +You are using `$(((` (or `(((`) to mean `$( ((`: command expansion with an arithmetic command. The more common interpretation is `$(( (`: arithmetic expansion with parentheses. + +This is an ill-defined structure that is parsed differently between different shells and shell versions. Prefer adding a space to make it unambiguous, both to shells and humans. + +Consider the `$(((` in `$(((1)) )`: + +Ash, dash and Bash 1 parses it as `$(( (` and subsequently fail to find the matching `))`. Zsh and Bash 2+ looks ahead and parses it as `$( ((`. Ksh parses it as `$( ( (`. + +### Exceptions: + +None. \ No newline at end of file