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

Re: Issue with VAR=foo cmd where VAR is a named reference



On Mon, May 4, 2026 at 6:19 PM Philippe Altherr
<philippe.altherr@xxxxxxxxx> wrote:
>>
>> Hmm, there's a complication here where you have to consider that (per
>> POSIX) some builtin commands preserve assignment to prefix environment
>> strings and some do not.  I don't immediately recall which category
>> "typeset" falls into.
>
>
> I didn't know about that but the code to save and restore the assigned parameters is indeed conditional. If POSIXBUILTINS is enabled, typeset preserves the assignments:
>
> % setopt POSIXBUILTINS; var=foo; var=bar typeset -p var; typeset -p var
> typeset var=bar
> typeset var=bar
>
>> > Exporting named references is in principle possible
>>
>> I think that would be a bad choice.  For security reasons it should
>> not be possible to import a named reference, and other programs would
>> have no idea what to do with an environment instrumented by zsh for
>> the purpose.
>
>
> I don't mean that it should be possible to export named references as something special that would be recognized as a named reference by a Zsh shell. I just mean that tagging a named reference R with -x could have for effect to augment the environment with the key value pair R=$R (if R refers to a non-array variable):
>
> % typeset var=foo
> % typeset -nx ref=var
> % printenv | grep ref=
> ref=var
> % zsh -c 'typeset -p ref'
> export ref=var
>
> In principle, this is trivial. In practice, it's difficult because the environment is precomputed. Each time an exported scalar is updated, its associated key value pair is updated in the precomputed environment. For exported named references, you would have to go through the whole parameter table each time a scalar is updated just to find whether there are exported named references that refer to it and update their associated key value pairs in the precomputed environment. That would be prohibitively expensive.

I would probably expect the result of the above code to be that the
environment contained ref=foo. Is that what you meant to type? I can't
see how the problem you describe would happen otherwise.

-- 
Mikael Magnusson




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