Email List: Xaustin-group-lX
[All Lists]

Re: Here-documents in $(...)

To: Geoff Clare <gwc@xxxxxxxxxxxxx>
Subject: Re: Here-documents in $(...)
From: Eric Blake <ebb9@xxxxxxx>
Date: Wed, 27 May 2009 13:11:12 -0600
Cc: austin-group-l@xxxxxxxxxxxxx
References: <4A15E71C.3070202@sonic.net> <200905261440.n4QEep5H018582@penguin.research.att.com> <20090527095232.GA18259@squonk.masqnet> <4A1D25C9.5000907@byu.net> <20090527143710.GB28819@squonk.masqnet>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Geoff Clare on 5/27/2009 8:37 AM:
> Eric Blake <ebb9@byu.net> wrote, on 27 May 2009:
>> It would be worth having the standard specify whether both of
>> these examples are well-behaved (and print hi) or syntax errors:
>>
>> echo $(
>> cat <<\)
>> hi
>> )
>> )
>>
>> echo $(
>> cat <<EOF
>> hi
>> EOF)
> 
> The standard is already clear that both of these must print hi.

I disagree - the standard as it currently stands is only clear that the
first must print hi:

cat <<\)
hi
)

is a well-formed here-doc.

But the second is questionable: the current documentation for here-docs
states "The here-document shall be treated as a single word that begins
after the next <newline> and continues until there is a line containing
only the delimiter and a <newline>".  Since there is no newline after the
EOF token, one could argue that the here-doc is unterminated, and that the
enclosing $( has no matching ), and that this is a syntax error rather
than printing hi.

My concern is that if you change here-docs to allow end-of-input to be a
valid terminator, then the first becomes ambiguous - is it a command
substitution of:

cat <<\)
hi
END_OF_INPUT

followed by an unmatched ')', or does the command substitution not end
until the second ')'?  On the other hand, by allowing end-of-file to
terminate a here-doc, then the second example is no longer a syntax error
- - the line with "EOF)" is treated as the here-doc terminator, end of
input, then the closing ')' for the command substitution.

Meanwhile, most shells that I tested output 'hi' for this case (showing
that end-of-input is already special-cased for here-docs in many shells),
although posh complained:

$ bash -c 'cat <<EOF
hi'
hi
$ posh -c 'cat <<EOF
hi'
posh: here document `EOF' unclosed

- --
Don't work too hard, make some time for fun as well!

Eric Blake             ebb9@byu.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkodkFAACgkQ84KuGfSFAYCIBwCgxxIs/kU704K+HWqqQGmyXr5s
TR4AoLRvvbEX7rZO96AhGYj6zW2lmc6J
=QmVz
-----END PGP SIGNATURE-----

<Prev in Thread] Current Thread [Next in Thread>