0

In the following code:

for i in {01..20};
do
    lxc start server$i
done 

I'd like to use a variable inside the curly braces but it doesn't work.

I tried this:

for i in {01..**$1**};
do
    lxc start server$i
done 

But the curly braces doesn't use the variable.

How can you solve this problem? The thing is that I need the sequence 01, 02, ...

josrrp
  • 23
  • 3
  • 2
    Brace expansion is done before variable expansion. Don't use brace expansion here. You can use `seq` instead. – user1934428 Nov 09 '22 at 12:54
  • The order of shell expansions is listed at [3.5 Shell Expansions](https://www.gnu.org/software/bash/manual/bash.html#Shell-Expansions) – glenn jackman Nov 09 '22 at 13:30
  • If you don't have `seq` (which is a non-standard utility) or want to avoid an external process, a simple `for` loop will work. You need to pair that with `printf` to get zero-padded numbers. `for ((i=1; i<=20; i++)); do printf -v server "server%02i" "$i"; lxc start "$server"; done` – tripleee Nov 09 '22 at 14:10

0 Answers0