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

Fwd: command substitutions starting with $((

Tail of a fairly interesting thread on the austin-group (POSIX) list.

---------- Forwarded message ----------
From: Geoff Clare <gwc@xxxxxxxxxxxxx>
Date: Tue, Dec 22, 2009 at 3:34 AM
Subject: Re: command substitutions starting with $((
To: austin-group-l@xxxxxxxxxxxxx

Philip Guenther <guenther@xxxxxxxxx> wrote, on 21 Dec 2009:
> On Mon, Dec 21, 2009 at 9:53 AM, Geoff Clare <gwc@xxxxxxxxxxxxx> wrote:
> ...
> > Are you claiming there are text sequences such that $((text)) is
> > syntactically valid both as an arithmetic substitution and as a
> > command substitution, and it is not a "single subshell" command
> > substitution?
> >
> > Unless such text sequences exist, it seems clear that the shell is
> > required to distinguish between the two cases based on which one the
> > syntax is valid for.
> How about:
> cat=1 ; EOH=3; echo $(( cat <<EOH
> + ( (
> ) && ( cat <<EOH
> ) ) + 1 +
> ))

<Applause> We have a winner.  I'm impressed :-)

This means we do need to make a change to the standard.  The tricky
part is going to be deciding exactly what to change.

Since it's a simple matter for application writers to use "$( ("
instead of "$((" when they are writing a command substitution
that starts with a subshell, I think it would be appropriate for
the standard to say that arithmetic expansion has precedence.
I.e. if $(( introduces a syntactically valid arithmetic expansion,
then an arithmetic expansion will be performed.  Otherwise if $((
introduces a syntactically valid command substitution, then a
command substitution will be performed.  If the syntax is valid
for neither, then it is unspecified what kind of syntax error the
shell reports.  (Or a modification of this which retains the
"single subshell" rule.)

This appears to match what current shells do.  All the shells I
tried Philip's example in output 256.

Geoff Clare <g.clare@xxxxxxxxxxxxx>
The Open Group, Thames Tower, Station Road, Reading, RG1 1LX, England

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