mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2051 (markdown)
25
SC2051.md
Normal file
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)
|
Reference in New Issue
Block a user