You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Vidar Holen edited this page Jun 28, 2018
·
1 revision
Use $((..)) for arithmetics, e.g. i=$((i + 2))
Problematic code:
i=3
i=i + 2
Correct code:
i=3
i=$((i +2))
Rationale:
Unlike most languages, variable assignments in shell scripts are space sensitive and (almost) always assign strings.
To evaluate a mathematical expressions, use $((..)) as in the correct code:
i=$((i + 2)) # Spaces are fine inside $((...))
In the problematic code, i=i + 2 will give an error +: command not found because the expression is interpreted similar to something like LC_ALL=C wc -c instead of numerical addition: