Brace expansion with a Bash variable - {0..$foo}
Solution 1:
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:
-
eval
is evil - use
eval
carefully
Here, $((..))
is used to force the variable to be evaluated as an integer expression.
Solution 2:
Curly braces don't support variables in BASH, you can do this:
for (( c=0; c<=WEEKS_TO_SAVE; c++ ))
do
mkdir -p weekly.${c}
done
Solution 3:
Another way of doing it without eval and calling mkdir only once:
WEEKS_TO_SAVE=4
mkdir -p $(seq -f "weekly.%.0f" 0 $WEEKS_TO_SAVE)
Solution 4:
Brace expansion does not support it. You will have to do it using a loop.
Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly textual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces. To avoid conflicts with parameter expansion, the string ‘${’ is not considered eligible for brace expansion
.