Skip to content
koalaman edited this page Mar 11, 2014 · 10 revisions

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)

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