SHELL: UNIX : Ls regular expression not working when used with variables

If i do below command in unix prompt which static values (ie 27..97), it is working fine and gives desired output

>ls -d $WORKDIR/batch/somefilename_{27..97}.* 2>/dev/null
somefilename_27.sometxt
somefilename_28.sometxt
somefilename_29.sometxt
..
somefilename_97.sometxt

But if i want to include variables or pass arguments to regular expression then its giving me error "ls: cannot access /home/work/batch/somefilename_{27..96}.*: No such file or directory". But thats not true bec file is present but somehow with variables regex is not working.

>segStart="27"
>segEnd="96"
>myvar="$segStart..$segEnd"

>echo $segStart
27
>echo $segEnd
96
>echo $myvar
27..96

ls -d $WORKDIR/batch/somefilename_{$myvar}.*
"ls: cannot access /home/work/batch/somefilename_{27..96}.*: No such file or directory"
>array=($(ls -d $WORKDIR/batch/somefilename_$myvar.*  2>/dev/null))
>len=${#array[*]}
>echo $len
0

Can someone please advise here why the regular expression is not working when using ls and {..} with variables?

Note: I am trying to store all the directory names in an array whose directory name is between two integer number
for eg there are 1-100 dir available with name file_1.some file_2.some file_3.some .. file_100.some.
If user wants to get directory from 47 till 97, then i want to read that value, store them and pass it in above ls command.

If you have any other alternative that will also help.

Millions thanks in advance guys!

Hi

eval ls -d $WORKDIR/batch/somefilename_{$myvar}.*
3 Likes

Thank you so much!!!!
It worked, really appreciate your prompt reply and your help :slight_smile: