Re: shell quoting (was: Improving the performance of system boot by eliding unused rc.d scripts and caching the result)

Ken Hornstein <[email protected]>
Newsgroups gmane.os.netbsd.devel.userlevel
Message-ID <[email protected]>
>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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.