>Suppose you have:
>
>   allf=$(ls *.jpg)
>   for f in $allf; do
>       printf 'File: %s\n' "$f"
>   done
>
>Now imagine that the files in $CWD are foo.jpg, bar.jpg, and silly
>filename with spaces.jpg...
>
>There is no easy way to make it more robust, unfortunately.

My limited, imperfect understanding is that in _this_ particular case,
this is safe:

    for f in *.jpg
    [...]

Since word splitting happens before pathname expansion.

If you need to save the results of the pathname expansion, you could do:

    set -- *.jpg

And then use "$@", as that expansion is special.  The downside is
that overwrites the argument list.  However, I am not sure there is
an portable way of dealing with the output of a command that may
contain spaces; you can change the value of IFS to remove spaces, in
the original example you would be tripped up if a filename contains a
newline (assuming you left a newline in IFS).

--Ken

Reply via email to