0

I am trying to use bash to list the files in the current directory with pattern *{num1..num2}.txt

I have tried to use the bash brace feature

ls *{10..20}.txt

, which works. However when I set the variable num1 and num2 first and then use

num1=10
num2=20
ls a{${num1}..${num2}}.txt

, which actually fails. I have used bash -x to debug and I found that bash automatically adds single quote to the string a{${num1}..${num2}}.txt, which is very weird.

Do you have any solution for this?

Thanks.

xin
  • 3
  • 2
  • 2
    Ignore the accepted answer if at all possible. The use of `eval` really isn't necessary (or a good idea when avoidable). You can accumulate the entries with the loop in an array if you need to run a single command with them all as arguments. – Etan Reisner Mar 30 '16 at 20:44
  • Do you mean the following loop? `all=; for i in {10..20}; do all="$all $(ls *$i.nc)"; done; echo $all` – xin Mar 30 '16 at 21:02
  • 1
    That loop is wrong for different reasons.`all=(); for i in {10..20}; do all+=( *"$i.nc" ); done; echo "${all[@]}"`. – chepner Mar 30 '16 at 22:01

1 Answers1

0
$ n=3; echo {"$n"..5}
{3..5}

so you need to evaluate the outcome

$ n=3; eval echo {"$n"..5}
3 4 5
karakfa
  • 66,216
  • 7
  • 41
  • 56