Skip to content
scottdcoulter edited this page Mar 28, 2022 · 10 revisions

Bash doesn't support variables in brace range expansions.

Problematic code:

for i in {1..$n}
do
  echo "$i"
done

Correct code:

for ((i=1; i <= n; i++))
do
  echo "$i"
done

Rationale:

In Bash, brace expansion happens before variable expansion. This means that brace expansion will not account for variables.

For integers, use an arithmetic for loop instead. For zero-padded numbers or letters, use of eval may be warranted:

from="a" to="m"
for c in $(eval "echo {$from..$to}"); do echo "$c"; done

or more carefully (if from/to could be user input, or if the brace expansion could have spaces):

from="a" to="m"
while IFS= read -d '' -r c
do
  echo "Read $c"
done <  <(eval "printf '%s\0' $(printf "{%q..%q}.jpg" "$from" "$to")")

Exceptions

None (if you're writing for e.g. zsh, you can use a directive to disable this check)

Related Resources:

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally