I ran into a problem I find rather strange and I hope some can explain it. I set up a simple bash for loop that iterates a few numbers and copies files that has each number two times in the file name. This fails when not using ${var_name} but succeeds when doing so. Here is a short example explaining what I mean:
$ touch 123.foo.123bar
$ touch 456.foo.456bar
$ mkdir out
$ for i in {123,456}; do cp $i.foo.$ibar out; done
cp: cannot stat `123.foo.': No such file or directory
cp: cannot stat `456.foo.': No such file or directory
$ for i in {123,456}; do cp ${i}.foo.${i}bar out; done
$ ls out
123.foo.123bar 456.foo.456bar
What is the reason that the first foor lopp fails while the second does not?