bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Issue declaring an array via a variable name


From: Alex fxmbsw7 Ratchev
Subject: Re: Issue declaring an array via a variable name
Date: Sun, 22 Aug 2021 22:38:22 +0200

as i said how to make it work, last time again, one pair quotes, not more

On Sun, Aug 22, 2021, 22:37 Chet Ramey <chet.ramey@case.edu> wrote:

> On 8/21/21 6:02 PM, Hunter Wittenborn wrote:
> >> As an end user I would expect the unquoted `('
> >
> >> operator to cause a syntax error, just as it does in `echo ('.
> >
> >
> >
> > Well I'm expecting '(' to be part of the shell's syntax (when unquoted;
> so likewise not cause a syntax error), but when looking at things like the
> left side of a variable assignment, I'm sure you'll agree that it should
> allow any string that fits a variable's normal specification (regardless of
> being an array or not).
>
> Maybe this is best seen as a misunderstanding about order of operations.
>
> Before the `declare' builtin is executed, the command has to be parsed.
> The parser identifies the token `declare' as the first word of a simple
> command, and further identifies `declare' as a declaration command, as
> explained in the documentation.
>
> One thing about the shell's metacharacters (of which `(' is one) is that
> they have to be quoted when they appear somewhere outside where the shell's
> grammar permits. One place bash allows `(' -- an extension to the POSIX
> grammar -- is on the rhs of an assignment statement.
>
> Given a declaration command, the shell parser allows assignment statements
> as arguments. An assignment statement, as documented, takes the form
>
> identifier=value
>
> where `identifier' is a `name' (as defined in the bash documentation) or an
> array subscript of the form `name[index]'.
>
> The key to understanding this is that all of this must happen before any of
> the shell's word expansions, including quote removal, take place.
>
> If the shell parser can't recognize a word, or at least the word that's
> been accumulated when it sees the `(' operator, as a valid assignment
> statement, the exception to having to quote the left paren doesn't come
> into play.
>
> We can disregard arguments that contain `=' that may be treated as
> assignments when `declare' sees them as long as they don't contain any
> shell metacharacters:
>
> >
> > `declare "${value}"="x"`
> >
> > `declare "y"="x"`
>
> However, if you want the parser to allow an unquoted metacharacter, you
> have to follow the rules that allow it to happen, and this does not:
>
> > `declare "${value}"=("x" "z")`
>
> Because "${value}" is not a valid shell `name' and cannot appear on the
> lhs of an assignment statement.
>
> Given that, it should be obvious why the above is a syntax error.
>
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
>                  ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]