0

I have the following shell script

cat test.sh    

j=00000001;
k=00000005;
l=$(echo {00000001..00000005}.jpg);
m=$(echo {$j..$k}.jpg);
ls $l
ls $m

Here is the output

 ./test.sh 
00000001.jpg  00000002.jpg  00000003.jpg  00000004.jpg  00000005.jpg
ls: cannot access {00000001..00000005}.jpg: No such file or directory

My doubt is "Why is the ls $m not working". and How to make that work?

Thanks in advance. lin

xeno xen
  • 3
  • 1

1 Answers1

1

Sequence expansion only happens for literal numbers. Variable expansion occurs after sequence expansion:

A sequence expression takes the form {x..y}, where x and y are either integers or single characters. When integers are supplied, the expression expands to each number between x and y, inclusive. When characters are supplied, the expression expands to each character lexicographically between x and y, inclusive. Note that both x and y must be of the same type.

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.

For your case, you can use eval:

m=`eval echo {$j..$k}.jpg`
Community
  • 1
  • 1
SheetJS
  • 22,470
  • 12
  • 65
  • 75
  • Thanks,that clarifies it.now i need to think of other ways to implement what i need.My requirement is to process a small sequential subset of numbered jpg files from a large set of jpg files and incrementing the subset after each processing. – xeno xen Oct 16 '13 at 05:23
  • You can use eval: m=`eval echo {$j..$k}.jpg` – SheetJS Oct 16 '13 at 05:29
  • i cannot find a `man eval` in my bash.But the command is there and it worked. – xeno xen Oct 16 '13 at 05:41
  • @xenoxen since its a shell builtin, you should run `help eval` or search `man bash` for `eval ` – SheetJS Oct 16 '13 at 05:45