Re: Array not defined when being set inline

2017-10-10 Thread Chet Ramey
On 10/10/17 9:06 AM, Dan Douglas wrote:

> Bash parses the array assignment as though it were valid while reading
> words, assignments, and redirects of the command. 

Because at the time the parser reads the assignment, it has to assume
that it's a candidate for compound assignment.  It's only when it
reads a non-assignment word that it becomes a temporary assignment
preceding a simple command.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: Array not defined when being set inline

2017-10-10 Thread Greg Wooledge
On Tue, Oct 10, 2017 at 09:05:55AM -0400, shawn wilson wrote:
> Nope, I knew how to correct it (as I showed in the last example), I
> just figured the other two should work.

"Work" how?  You placed a variable in the TEMPORARY execution ENVIRONMENT
of a command.  You can tell it's an environment variable by the "-x"
in the output of declare.  And since it's an environment variable, it
is therefore a string variable, because you can't export arrays.

And it doesn't persist because it's a TEMPORARY variable.  It's just
there for the duration of that one command.

What are you trying to do?



Re: Array not defined when being set inline

2017-10-10 Thread Dan Douglas
On 10/10/2017 07:00 AM, shawn wilson wrote:
> I guess that's the right way to describe what I'm seeing:
> 
> [swilson@localhost ~]$ unset f; f=(aaa bbb ccc) declare -p f
> declare -x f="(aaa bbb ccc)"
> [swilson@localhost ~]$ unset f; f=("aaa" "bbb" "ccc") declare -p f
> declare -x f="(aaa bbb ccc)"
> [swilson@localhost ~]$ unset f; f=(aaa bbb ccc)
> [swilson@localhost ~]$ declare -p f
> declare -a f='([0]="aaa" [1]="bbb" [2]="ccc")'
> 
> Is this known? What exactly is going on / what does this mean?
> 

Bash parses the array assignment as though it were valid while reading
words, assignments, and redirects of the command. After processing
expansions, the saved assignment text is re-interpreted as a string
assignment, presumably because at this point bash no longer cares about
the quoting of metacharacters so they're just treated as literal.

Array assignments aren't valid preceding commands of course because
the environment fundamentally stores strings / key-value pairs of
binary blobs.

This should probably be an error, but this is what bash has always done,
and this hasn't really caused any problems other than occasionally
tricking people into thinking arrays are somehow exportable.



signature.asc
Description: OpenPGP digital signature


Re: Array not defined when being set inline

2017-10-10 Thread shawn wilson
On Tue, Oct 10, 2017 at 8:21 AM, Greg Wooledge  wrote:
> On Tue, Oct 10, 2017 at 08:00:58AM -0400, shawn wilson wrote:
>> I guess that's the right way to describe what I'm seeing:
>>
>> [swilson@localhost ~]$ unset f; f=(aaa bbb ccc) declare -p f
>> declare -x f="(aaa bbb ccc)"
>
> You placed a string variable in the temporary execution environment of
> the declare command.  If you wanted an actual array variable that would
> persist past this command, you need a semicolon or newline after the
> assignment, and before the declare command.
>

Nope, I knew how to correct it (as I showed in the last example), I
just figured the other two should work.

>
> See also: http://mywiki.wooledge.org/BashFAQ/104

Thanks for that (and the link it provides to BashParser - I'm going to
have to digest that).

Is there a way to detect that the parser has already processed a
temporary variable and maybe change the exit status or something?



Array not defined when being set inline

2017-10-10 Thread shawn wilson
I guess that's the right way to describe what I'm seeing:

[swilson@localhost ~]$ unset f; f=(aaa bbb ccc) declare -p f
declare -x f="(aaa bbb ccc)"
[swilson@localhost ~]$ unset f; f=("aaa" "bbb" "ccc") declare -p f
declare -x f="(aaa bbb ccc)"
[swilson@localhost ~]$ unset f; f=(aaa bbb ccc)
[swilson@localhost ~]$ declare -p f
declare -a f='([0]="aaa" [1]="bbb" [2]="ccc")'

Is this known? What exactly is going on / what does this mean?