Tuesday, February 28, 2023

Bash: Looping Over Filenames With Spaces

 The common:

for F in *; do ...; done

will fail if the files contain whitespace in their names. At least for files with just spaces, this works:

find -iname GLOB | while read F; do
   ....
done

https://stackoverflow.com/questions/7039130/iterate-over-a-list-of-files-with-spaces

 


UPDATE: It appears that newer versions of bash solve this problem and do what expect it to do:

#> for i in h*; do echo $i; done
ho ho