6

Tip #67   Loop over files with spaces in the name

Loop over files with spaces in the name:

(Wrong) example:
$ touch a

$ touch b\ c
$ for i in *; do ls $i; done;
a
ls: b: No such file or directory
ls: c: No such file or directory


The trick is to change the separator ($IFS) to newline:
old_ifs=$IFS

IFS="
"
$ for i in *; do ls $i; done;
a
b c