Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Reading line by line

Stephane Chazelas wrote:
On Wed, Apr 11, 2007 at 11:13:21AM -0700, Micah Cowan wrote:
sac wrote:
2. Like Micah J. Cowan pointed out,
while read i
echo $i
done < filename
As Stephane points out, -r is preferable, as it disables
backslash-escaping. IFS= should not be necessary--and isn't, in zsh--but
dash, bash and ksh all violate POSIX (as I read it) by inserting new
field separators after splitting, instead of using the original
separation; so using IFS= is portable.

Don't know what you mean, but IFS= is necessary even with zsh,
and the behavior is POSIX in every shell AFAICT, could you
please expand.

$ echo '  a  ' | read -r
$ echo "<$REPLY>"
$ echo '  a  ' | IFS= read -r
$ echo "<$REPLY>"
<  a  >

Good point, I hadn't been thinking about whitespace at the beginning of the line.

My assertion wrt POSIX was in the case of something like:

$ echo 'a   b' | read -r

zsh will preserve all whitespace between a and b, whereas other shells will condense it to a single space. POSIX states that, after splitting the fields, if there is only one variable name left in the args, all remaining fields will be assigned to that variable, with "their intervening separation", which IMO is difficult to interpret as anything other than the original separation that had been encountered.

Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...

Messages sorted by: Reverse Date, Date, Thread, Author