[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Why `echo -n hello | while read v; do echo $v; done' prints nothing?
From: |
Eric Blake |
Subject: |
Re: Why `echo -n hello | while read v; do echo $v; done' prints nothing? |
Date: |
Thu, 02 Dec 2010 20:11:09 -0700 |
User-agent: |
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.12) Gecko/20101103 Fedora/1.0-0.33.b2pre.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.6 |
On 12/02/2010 07:02 PM, Clark J. Wang wrote:
>> The output from the first command in the pipeline does not end with a
>> newline. Therefore, 'read' in the second command returns 'failure'
>> (non-zero) when it reads the first line of input, and your loop never
>> iterates.
>
> But is that reasonable? I think read should return success in this case
> which makes more sense to me. Does the POSIX standards require that?
POSIX requires that the input to read be a text file (and by the
definition of text file in POSIX, it must either be empty or end in a
newline). By violating POSIX and passing something that does not end in
a newline, you are no longer bound by the rules of POSIX. Therefore, it
would be a reasonable bash extension that read could return 0 status if
it read data that did not end in a newline, but it would not be a
standard-compliant script that relied on such an extension. You're
better off supplying the trailing newline, and guaranteeing a compliant
usage.
--
Eric Blake eblake@redhat.com +1-801-349-2682
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature