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

Re: Zsh noob: word-splitting headache



On Tue, Jan 04, 2005 at 01:30:44PM -0800, Bart Schaefer wrote:
[...]
>    foo | while read i; do something with $i; done

foo | while IFS= read -r i; do something with "$i"; done

or leading and trailing blanks would be stripped, and backslash
would be treated specially.

Note that this means that "something" stdin is modified.

The quotes around "$i" are here in case there are empty lines.

Note that in

for i in ${(f)"$(foo)"}

or

IFS=$'\n'
for i in $(foo)

The empty lines are discarded.

To avoid that, you can do:

IFS=$'\n\n'
for i in $(foo)

But still, because of the (to my mind bogus) way command
substitution works in Bourne like shells, the empty lines at the
end of foo output will still be discarded. To prevent that, you
can do:

IFS=$'\n\n'
for i in ${$(foo; echo .)[1,-2]}

$ printf '<%s>\n' ${$(echo 'a b\n\n'; echo .)[1,-2]}
<a b>
<>
<>

-- 
Stéphane



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