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

Re: Reading line by line

On Wed, Apr 11, 2007 at 11:52:41AM -0700, Micah Cowan wrote:
> $ 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.

No, you must have been confused by word splitting occuring upon
unquoted variable expansions.


echo 'a  b' | read a
echo $a

You see "a b", not because "read" compresses the spaces, but
because $a is word split into 2 arguments "a" and "b", and
echo outputs its parameters separated by *one* space.

echo 'a  b' | read a
echo "$a"

outputs "a  b"

That's what POSIX requires and that's how zsh behaves when
in POSIX emulation.

When not in POSIX emulation, zsh behaves (almost) the intuitive
way, that is

echo $a

does pass the content of $a as a single argument to echo which
is the content of $a. I said "almost" because it's only true if
$a is set and not empty (and of course if it's of scarlar type).

Note that word splitting is not the only thing performed by
POSIX shells upon variable expansion, there's also filename
generation. That's why one should *NEVER* leave variables
unquoted in POSIX scripts.

In zsh, to have word splitting:

$=a instead of $a ($= look like scissors).

$~a to have filename generation.

So $a in POSIX shells is like $=~a in zsh.


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