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

Re: Obscure positional-parameter assignment feature/bug



2025-11-09 14:15:49 -0800, Bart Schaefer:
> Consider:
> 
> % () { 03=foo; print -l $argv } 1 2 3 4
> 1
> 2
> foo
> 4
> %
> 
> Do we want to allow leading zeros in the "names" of positional
> parameters?  I rather expect this was not intentional and it seems
> unlikely anyone is relying on it.  On the other hand prohibiting it is
> presently a mildly annoying special case.
> 
> Tangentially, reference to argv[03] or even say argv[2#011] bypasses
> any such restriction because the subscript is interpreted as an
> integer.


AFAICT, all shells (Bourne-like ones, but also csh or rc)
treat ${01} the same as $1 (and ${010} the same as $10, not $8).
That's even an explicit POSIX requirement:
https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/utilities/V3_chap02.html#tag_19_05_01

For zsh, that means ${01::=foo} should also assign to $1, and
allowing 01=foo would be consistent with that.

Though unlikely, if anyone does 01=foo, I can't think of
anything else they would mean by that, so I see no harm in
allowing it.

AFAIK, zsh is the only shell that allows 1=value.

In plan9 rc (as opposed to Byron's), that's accepted but sets
the 1 env var, not positional parameter.

$ plan9-rc -c '1 = foo; echo 1: $1; printenv 1;  *=(bar); echo 2: $1; printenv 1'
1:
foo
2: bar
foo

$ byron-rc -c '1 = foo'
rc: line 0: numeric variable name

(AFAIK, in rc, you can't assign array elements or slices anyway,
only the array as a whole).

In fish, positional parameters are in $argv only. You can assign
to 1, but that's a $1 variable, not $argv[1]

$ fish -c 'set 1 a b; set argv c d; echo "$1[1] | $1 | $argv[1]"'
a | a b | c


-- 
Stephane




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