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

Re: Bug with unset variables



On Wed, Nov 11, 2020 at 4:57 PM Felipe Contreras
<felipe.contreras@xxxxxxxxx> wrote:
>
> Hello,
>
> It's obvious what this code should do:
>
>   foo () {
>     typeset var
>     echo "var: '${var-other}'"
>   }

When foo is invoked, based on the documentation for typeset, I would
expect it to print "var: ''".

    Except as noted below for control flags that change the behav-
    ior, a parameter is created for each name that does not already
    refer to one.

And indeed that's how foo behaves when I invoke it in zsh 5.8.

> I also tried with the other unnamable shell, and the correct output is shown.

I confirm that in bash 5.0.17 function foo prints "var: 'other'".
However, I'm not sure whether this behavior agrees with the
documentation.

From `help declare`:

    Declare variables and give them attributes.

    [...]

    When used in a function, `declare' makes NAMEs local, as with the `local'
    command.

From `help local`:

    Create a local variable called NAME, and give it VALUE.

As far as I can tell, there is no indication anywhere in the
documentation that omitting =VALUE will cause `local` to not create a
variable in bash.

Do you believe the behavior of bash is expected while zsh behaves
incorrectly? If so, why?

Roman.




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