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

Re: More rabbit-holes with unset variables



My use of "rabbit holes" has been even more prophetic than I expected.
(Ironically, spellcheck wants to correct that to "pathetic".)

On Thu, Nov 26, 2020 at 5:51 PM Felipe Contreras
<felipe.contreras@xxxxxxxxx> wrote:
>
> On Thu, Nov 26, 2020 at 6:23 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> >
> > The point is that the exported value DOES NOT exist in this example;
> > if you were to look at the C global "environ" array following "export
> > FOO", it would not have (a pointer to a string containing) "FOO" in
> > it.
>
> How do you know?

By paying attention to context?

We're not talking about Oliver's example at this point, we're talking
specifically about the case where "export FOOBAR" creates a global
variable that did not exist before.  If it did previously exist, then
the internal and environment namespaces would be the same.  The only
way for them to differ is when FOOBAR did not already exist.

> You used precisely this argument when I brought up this example:

Not the same, because the behavior of export vs. local (typeset in
function context) is not the same; the latter always creates a new
variable, and that variable doesn't inherit from scope.

> The inconsistency between the internal and external value *only*
> happens in zsh, and it most definitely exists.

I have never denied that.  What I said was that if you never leave
zsh, you can't tell.  That you can tell by forking off /bin/sh is
because of the way the internal and external namespaces are managed,
not because of the way the internal namespace works, and I'm
unsuccessfully attempting to keep this thread focused on the latter.

> To be consistent, either these two are the same:
>
>   typeset -x FOO
>   typeset -x FOO=""
>
> Or these two are different:
>
>   typeset FOO
>   typeset FOO=""

In the internal namespace, and starting from a name FOO that's never
been used/does not appear in the process environment, in zsh all of
these do the same thing:

declare FOO
local FOO
export FOO

To use Daniel's "${verb}s a variable" from the other thread, ${verb}
is 'creates an internal parameter to represent'.  The entirety of
these two discussion threads is supposed to be about when it is
appropriate for that to have a default value, and what it means for it
not to; currently it always has one.

Specifically for "export" there are two additional requirements:
1) If the variable already exists with a value, then variable=value is
added to the environment.
2) If a value is later assigned to the variable, then variable=value
is added to the environment.

Neither of those requirements is met under the stated conditions, so
nothing is added to the environment.  This is entirely separate from
whether there is a default.

Again starting from a previously nonexistent FOO, all of these are
also the same:

declare FOO=anything
local FOO=anything
export FOO=anything

These explicitly do two things:  First ${verb}, and then assign.  So
for export, the second additional requirement is met, and
variable=value is added to the environment.

Is there an inconsistency from the viewpoint of an omniscient
observer?  Yes.  As a practical matter, can either a script written
entirely in zsh, or an external program invoked from zsh,
independently discern this inconsistency?  No.

I'm done with responses about export behavior on this thread except
when directly related to the treatment of default values.




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