Looping through filenames with spaces

I need to loop through the files in a directory and process the files. But some of the filenames contain spaces.

Here is a little test script I've been using to experiment. (I'm not really going to call 'echo', I'm doing some other processing.) Everything I try fails. How can I do this??

#!/bin/sh
# spacetest
DIR="/tmp/spacetest$$"
rm -rf $DIR
mkdir -p $DIR || exit 1
touch $DIR/{"a b","c d"}

# This works, when I itemize filenames a priori
set "a b" "c d"
for x in "$@"; do
    echo $x
done

echo Test 1:    # Fails
for x in $(find $DIR -mindepth 1 -printf '"%f" '); do
    echo $x
done

echo Test 2:    # Fails
set $(find $DIR -mindepth 1 -printf '%f ')
for x in "$@"; do
    echo $x
done

echo Test 3:    # Fails
set $(find $DIR -mindepth 1 -printf '"%f" ')
for x in "$@"; do
    echo $x
done

echo Test 4:    # Fails
_list="$(find $DIR -mindepth 1 -printf '"%f" ')"
set $_list
for x in "$@"; do
    echo $x
done

rm -rf $DIR

Assuming there are no newlines in any filenames:

find $DIR -mindepth 1 -printf '"%f" ' |
while IFS= read -r file
do
  echo "$x"
done

use of quotes and use of IFS

> cat see_blanks
ls -l file* 
ls | grep "file" >f_list

while read filename
  do
  echo "### echo"
  echo $filename
  echo "### ls"
  ls $filename
  echo "### ls in quotes"
  ls "$filename"
  echo "### play with IFS"
  OLDIFS=$IFS
  IFS="~"
  ls $filename
  IFS=$OLDIFS

done<f_list

prorgam execution

> see_blanks
-rw-rw---- 1 xxx dp 0 Nov 28 10:04 file 04
-rw-rw---- 1 xxx dp 0 Nov 28 10:04 file01
### echo
file 04
### ls
ls: cannot access file: No such file or directory
ls: cannot access 04: No such file or directory
### ls in quotes
file 04
### play with IFS
file 04
### echo
file01
### ls
file01
### ls in quotes
file01
### play with IFS
file01

So, if I use the variable inside quotes, it works. And if I reset the IFS setting then I do not break-apart the commands at each space.

I made some progress with input from this forum, thank you, but I'm down to one stick point. Consider this simpler script:

#!/bin/sh
VAR=
echo 'A A
B B
C C' | while read f; do
    VAR="$VAR:$f"
    echo $f
done
echo "VAR=${VAR}"

The output is

A A
B B
C C
VAR=

The f variable echos properly in the loop, but I just can't seem to assign it's value to any other variable.
Why not?

Each element of a pipeline is executed in its own subshell and therefore cannot affect the parent shell.

Try echo "$VAR" inside the loop, and you'll see that it does get assigned to.

Oh. How terribly inconvenient.

Oh, well. I may have stumbled upon a different solution:

$!/bin/sh
VAR="$(ls | tr '\n' :)"
echo "VAR=$VAR"

If I have files "A A", "B B" and "C C", I'll get this result:

VAR=A A:B B:C C:

Now I can work with VAR like this:

#!/bin/sh
while [ -n "$VAR" ]; do
    f="${VAR%%:*}"
    VAR="${VAR#*:}"
    # process "$f" ...
done

You don't need any external commands to do that:

VAR=$( printf "%s:" * )

In bash, 3.1 and later, you can do it with:

printf -v VAR "%s:" *

What's wrong with:

for f in *
do
    : # process "$f" ...
done

You are a man of many talents, cfajohnson.
I will try to put all this into action tomorrow.