how to print range of numbers by using passing value to variable from 1 to $num with echo command?

63 views Asked by At

I want to print all the integer value from 1 to 10 by using echo {1..$num} and i want to used it pdftk

If i do with echo {1..10} it is printing what i exactly want, but i want to pass a value to some variable and that variable below is my MWE.

  #!/bin/bash
  num=10
  echo {1..10} # This code printing correctly
  echo {1..$num} # This code not printing correctly
  pdftk {1..$num}.pdf cat output final.pdf

but it is prininting 1..10 only but i want to print 1 2 3 4 5 6 7 8 9 10

1

There are 1 answers

0
user1934428 On

Brace expansion is performed before parameter expansion. Therefore your approach does not work. You could do instead a

num=10
seq $num
for n in $(seq $num)
do
  pdftk $n.pdf
done 

If you want to avoid a loop, do instead a

pdftk $(seq $num | sed 's/$/.pdf/')

or

pdftk $(printf "%s.pdf " $(seq $num))

instead.