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

Re: [PATCH (not final)] (take three?) unset "array[$anything]"



2021-06-04 17:20:29 -0700, Bart Schaefer:
[...]
> > array[1 + 1]=4
> >
> > That is pretty annoying.
> 
> That's just shell whitespace rules, though.  If you have nobadpattern
> set, you get
> 
> zsh: command not found: array[1
> 
> And if the parser doesn't eventually find ]= then what?  Re-parse the
> whole thing to word-split it?
[...]

Note that all other shells that support a[1]=value (ksh, pdksh,
bash) do it.

In effect that makes them non-POSIX as there's nothing in the
spec POSIX that says

a[1 + 1]=2

May not run the a[1 with +] and =2 as arguments.

I did bring that up a few years ago at:
https://www.mail-archive.com/austin-group-l@xxxxxxxxxxxxx/msg04563.html
(see also https://austingroupbugs.net/view.php?id=1279)

There is variation in behaviour between shells if the first word
starts with name[ and either a matching ] or matching ] followed
by = is not found, and how that matching is done or what kind of
quoting are supported inside the [...].

But all in all, even if it may not be pretty when you look closely,
that does work fine there as long as you're not trying to fool
the parser.

In zsh, that would be even less problematic as (at least when
not in sh emulation), zsh complains about unmatched [s or ]s
(except for [ alone)

Even:

$ /bin/[ a = a ]
zsh: bad pattern: /bin/[

So we wouldn't be breaking anything if we started to accept:

hash[foo bar; baz]=x

or

array[1 + 1]=x

We would if we started to accept.

hash['x]y'-$'\n']=x

like ksh93/bash do though.

For literal hash[key]=value assignment, I never remember what needs
to be quoted and how. I generally resort to doing
var=key; hash[$var]=x
or
hash+=(key x)
which I know are fine.

-- 
Stephane




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