WEEKS_TO_SAVE=4
mkdir -p weekly.{0..$WEEKS_TO_SAVE}
gives me a folder called weekly.{0..4}
Is there a secret to curly brace expansion while creating folders I'm missing?
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 evileval
carefullyHere, $((..))
is used to force the variable to be evaluated as an integer expression.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With