Created SC2051 (markdown)

koalaman
2014-03-11 11:04:39 -07:00
parent 16d16a1536
commit 3ebc77ddba

25
SC2051.md Normal file

@@ -0,0 +1,25 @@
## Bash doesn't support variables in brace expansions.
### Problematic code:
for i in {1..$n}
do
echo "$i"
done
### Correct code:
for ((i=0; i<n; i++))
do
echo "$i"
done
### Rationale:
In Bash, brace expansion happens before variable expansion. This means that brace expansion will never account for variables.
Use an arithmetic for loop instead.
### Contraindications
None (if you're writing for e.g. zsh, make sure the shebang indicates this so shellcheck won't warn)