I have a folder with files named as file_1.ext...file_90.ext
. I can list a range of them with the following command:
$ ls /home/rasoul/myfolder/file_{6..19}.ext
but when I want to use this command inside a bash script, it doesn't work. Here is a minimal example:
#!/bin/bash
DIR=$1
st=$2
ed=$3
FILES=`ls ${DIR}/file\_\{$st..$ed\}.ext`
for f in $FILES; do
echo $f
done
running it as,
$ bash test_script.sh /home/rasoul/myfolder 6 19
outputs the following error message:
ls: cannot access /home/rasoul/myfolder/file_{6..19}.ext: No such file or directory
Brace expansion happens before variable expansion.
(Moreover, don't parse ls output.). You could instead say:
for f in $(seq $st $ed); do
echo "${DIR}/file_${f}.ext";
done