From 8f762997fe18ecbd7374f47f0d2ea871d15e0db6 Mon Sep 17 00:00:00 2001 From: koalaman Date: Thu, 23 Apr 2015 14:14:42 -0700 Subject: [PATCH] Created SC2084 (markdown) --- SC2084.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 SC2084.md diff --git a/SC2084.md b/SC2084.md new file mode 100644 index 0000000..bbb2764 --- /dev/null +++ b/SC2084.md @@ -0,0 +1,43 @@ +## Remove '$' or use '_=$((expr))' to avoid executing output. + +### Problematic code: + + i=4 + $(( i++ )) + +### Correct code: + +Bash, Ksh: + + i=4 + (( i++ )) + +POSIX (assuming `++` is supported): + + i=4 + _=$(( i++ )) + +Alternative POSIX version that does not preserve the exit code: + + : $(( i++ )) + +### Rationale: + +`$((..))` expands to a number. If it's the only word on the line, the shell will try to execute this number as a command name: + + $ i=4 + $ $(( i++ )) + 4: command not found + $ echo $i + 5 + +To avoid trying to execute the number as a command name, use one of the methods mentioned: + + $ i=4 + $ _=$(( i++ )) + $ echo $i + 5 + +### Exceptions: + +None. \ No newline at end of file