A simple variable subst is not working

Hi

what i want:
listing files in a special range

ls -lrt 20120601{05..06}*
 ...
 -rw-rw-r-- 1 imp imp 279  1. Jun 07:51 201206010550
 -rw-rw-r-- 1 imp imp 279  1. Jun 07:01 201206010600
 -rw-rw-r-- 1 imp imp 279  1. Jun 07:11 201206010610
 -rw-rw-r-- 1 imp imp 279  1. Jun 07:21 201206010620
 -rw-rw-r-- 1 imp imp 279  1. Jun 07:31 201206010630
 -rw-rw-r-- 1 imp imp 279  1. Jun 07:41 201206010640
 -rw-rw-r-- 1 imp imp 279  1. Jun 07:51 201206010650

.. setting the command like this, it is working properly.

If i subst one value by a variable or a command...

M=05
ls -lrt 20120601{$M..06}*
"note"
ls: no access to 20120601{05..06}* : cant find file or directory.
.. with the command ..
ls -lrt 20120601{`date +%m`..06}ls -lrt 20120601{`date --date="9 days ago" +%m`..06}
ls: no access to20120601{05..06}*cant find file or directory.

It seems to me, that the brackets are still there ..
A colleague gave me a first hint ..

ls -lrt `eval echo 20120601{$M..06}*`

Can someone help me, how to do it... more elegant and especially with the subst. commands.

Thanks in advance
IMPe

Actually the solution your colleague gave you is correct. The reason is that there is a certain step-by-step procedure which the shell follows when evaluating commands. Your code assumed that these steps are interchangeable, but they aren't.

This is where "eval" enters the picture: eval tells the shell to restart this process at step one and so - by evaluating your commandline in fact twice - it is possible to first evaluate "$M" and only then evaluate "{05..06}".

You might want to read this thread for a more detailed explanation. The symptom there was different but the problem was the same.

I hope this helps.

bakunin

1 Like