bug-bash
[Top][All Lists]
Advanced

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

Re: +=() can be used to set illegal indices


From: Emanuele Torre
Subject: Re: +=() can be used to set illegal indices
Date: Mon, 3 Jul 2023 23:39:41 +0200
User-agent: Mutt/2.2.10 (2023-03-25)

On Mon, Jul 03, 2023 at 05:24:49PM -0400, Chet Ramey wrote:
> It's really more like
> 
> a=(); a[0]=1 a[1]=2 a[2]=3
> 
> and the += variant omits the initial array flush.

Oh, yes. I knew it, but I forgot to mention it.

> > I would have expected:
> > 
> >    $ a=([1]=hi [100]=foo [-1002]=bar boo [200]=baz); echo "won't run"
> >    bash: [-1002]=bar: bad array subscript
> >    $ declare -p a
> >    bash: declare: a: not found
> > 
> > But it seems bash actually behaves like so:
> > 
> >    $ a=([1]=hi [100]=foo [-1002]=bar boo [200]=baz); echo "will run"
> >    bash: [-1002]=bar: bad array subscript
> >    will run
> >    $ declare -p a
> >    declare -a a=([1]="hi" [100]="foo" [101]="boo" [200]="baz")
> > 
> > So it simply skips and prints a warning for invalid indices, and still
> > sets all the other valid indices, without triggering an error for the
> > assignment; even though  a[-1002]=bar  on its own would have triggered
> > an error:
> > 
> >    $ a[1]=hi a[100]=foo a[-1002]=bar a[200]=baz; echo "won't run"
> >    bash: [-1002]=bar: bad array subscript
> >    $ declare -p a
> >    declare -a a=([1]="hi" [100]="foo")
> 
> So how about we make the behaviors converge a little bit better: compound
> assignment breaks on the first invalid assignment and makes the assignment
> statement fail, which can be treated as an assignment error.

Sounds good, I like that. Thank you!

o/
 emanuele6



reply via email to

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