You would be better served by using this structure:
while read fname
do
....
done < <(find ...)
Or, if you're not using bash
:
find ... | while read fname
do
....
done
The problem with storing the output of find
in a variable, or even doing for fname in $(find ...)
, is with word splitting on whitespace. The above structures still fail if you have a file name with a newline in it, since they assume that you have one file name per line, but they're better than what you have now.
An even better solution would be something like this:
find ... -print0 | xargs -0 -n1 -Ixxx somescript.sh "xxx"
But even that might have issues if filenames have quotes or other things in them.
The bottom line is that parsing arbitrary data (which filenames can be) is hard...