Brace expansion with a Bash variable – {0..$foo}

bash does brace expansion before variable expansion, so you get weekly.{0..4}.
Because the result is predictable and safe(Don’t trust user input), you can use eval in your case:

$ WEEKS_TO_SAVE=4
$ eval "mkdir -p weekly.{0..$((WEEKS_TO_SAVE))}"

note:

  1. eval is evil
  2. use eval carefully

Here, $((..)) is used to force the variable to be evaluated as an integer expression.

Leave a Comment