ksh bugs

Warren D. Swan woods at cbnewsc.ATT.COM
Wed Aug 9 04:58:21 AEST 1989


In article <10166 at fluke.COM> foot at tc.fluke.COM (Andrew Proudfoot) writes:
>2. the "read" command does filename expansion on its input! 
>   Just filename expansion; it doesn't do quote removal or tilde expansion.
>   This happens only on the Suns, not on the Vaxes.
>
>    $ ls /usr0/foot/xx
>    file1   file2   file3
>    $ ls ~foot/xx
>    file1   file2   file3
>    $ while read stuff; do echo $stuff; done << EOF
>    > ~foot/xx/*
>    > '/usr0/foot/xx/*'
>    > /usr0/foot/xx/*
>    > EOF
>    ~foot/xx/*
>    '/usr0/foot/xx/*'
>    /usr0/foot/xx/file1 /usr0/foot/xx/file2 /usr0/foot/xx/file3

The other two sound like problems, but this one definitely isn't.  This is
not the read statement doing filename expansion.  Nor is it, as someone
ludicrously suggested without checking, the here document doing filename
expansions, because the shell only does $ expansion on here documents
[unless the sentinel (EOF) is quoted, even partially ('EOF' or \EOF)], NOT
` or filename expansions.

The problem is in your reference to $stuff.  One of the first rules of shell
programming ought to be:  Always quote any expansion of an environment or
shell variable (unless you WANT something like the above to happen).

Just change your while loop to read:
$ while read stuff; do echo "$stuff"; done <<EOF
...

You see, the shell does filename expansion AFTER doing $ expansion.  So
$stuff is being replaced with /usr0/foot/xx/*, which is then expanded by
the shell, and the individual arguments are then handed to echo.

Try this:
JUNK='*'  # (I use quotes to delay another long discussion here, but they're
          # not needed.  JUNK=* would do the same thing.)
echo $JUNK  # "Weeeee. Lookah my files."
echo "$JUNK"  # "What a pretty little star!"

Have fun,
Warren D. Swan  (WooDS)     Y n n ____ __      You can't tell which way a train
AT&T Bell Laboratories     -(((((([__]/__]     went by looking at the tracks.
Naperville, Illinois       /o-OOOOO~~  oo
att!cblph!woods         #####################  FRISCO 1630 Decapod (2-10-0) IRM



More information about the Comp.bugs.sys5 mailing list