Difference between using ls and find to loop over files in a bash script
bash, find, ls, unix
Solution
The correct way to iterate over filenames here would be
for f in strain_flame_00*.dat; do
echo "$f"
mybase=$(basename "$f" .dat)
echo "$mybase"
done
Using `for` with a glob pattern, and then quoting all references to the filename is the safest way to use filenames that may have whitespace.
Problem
I'm not sure I understand exactly why: ``` for f in `find . -name "strain_flame_00*.dat"`; do echo $f mybase=`basename $f .dat` echo $mybase done ``` works and: ``` for f in `ls strain_flame_00*.dat`; do echo $f mybase=`basename $f .dat` echo $mybase done ``` does not, i.e. the filename does not get stripped of the suffix. I think it's because what comes out of `ls` is formatted differently but I'm not sure. I even tried to put `eval` in front of ls...