zsh-workers
 help / color / mirror / code / Atom feed
From: Felipe Contreras <felipe.contreras@gmail.com>
To: Roman Perepelitsa <roman.perepelitsa@gmail.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Bug with unset variables
Date: Wed, 11 Nov 2020 10:56:08 -0600	[thread overview]
Message-ID: <CAMP44s29X6VBRwpwVj-anXax0UU6WRnukmUqrpoweM_cqbDdTQ@mail.gmail.com> (raw)
In-Reply-To: <CAN=4vMrvB--NSrRJEyUaaxhyErgyF4=+0da+=TE2mU_3KXopyA@mail.gmail.com>

On Wed, Nov 11, 2020 at 10:13 AM Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> On Wed, Nov 11, 2020 at 4:57 PM Felipe Contreras
> <felipe.contreras@gmail.com> 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: ''".

My main concern is not the documentation of typeset, but useful code.

The primary reason for why typeset exists is to set the scope of a
variable. Especially so when using "local".

If I actually do something on the foo() function:

  typeset var
  [[ -n "$1" ]] && var=$1
  echo "var: '${var-other}'"

I would expect two things: 1) var is not set when I exit the function,
and 2) var is not set until I specifically set it.

I can get both in ksh and bash, but not in zsh. In zsh I have to
choose either 1) with typeset, or 2) by removing typeset.

How do you suggest I get both in zsh?

>     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.

It is possible to "create" a parameter without value (indeed that's
what happens in other shells), the documentation doesn't say anything
about assigning it a default empty string as value.

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

> 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.

The documentation makes it obvious that:

  local name

Creates a local variable called "name".

And:

  local name=value

Creates a local variable called "name", and gives it "value" as its value.

If you omit the value, then it doesn't assign it any value. As is the
case in countless languages; declaring a variable without specifying
any value assigns it the value of null (or "unset" in shell lingo).

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

Yes, and ksh also behaves correctly.

As stated above, it's the only behavior that makes sense.

Otherwise you cannot declare a local variable that is unset.

Cheers.

-- 
Felipe Contreras


  reply	other threads:[~2020-11-11 16:56 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-11 15:57 Felipe Contreras
2020-11-11 16:13 ` Roman Perepelitsa
2020-11-11 16:56   ` Felipe Contreras [this message]
2020-11-11 17:02     ` Roman Perepelitsa
2020-11-11 18:03       ` Felipe Contreras
2020-11-11 18:16         ` Roman Perepelitsa
2020-11-11 20:42           ` Felipe Contreras
2020-11-12  0:20             ` Mikael Magnusson
2020-11-12  1:10               ` Felipe Contreras
2020-11-12  8:45             ` Roman Perepelitsa
2020-11-12 10:47               ` Peter Stephenson
2020-11-12 18:48                 ` Bart Schaefer
2020-11-12 19:49                 ` Felipe Contreras
2020-11-12 18:46               ` Felipe Contreras
2020-11-12 19:10                 ` Roman Perepelitsa
2020-11-12 21:08                   ` Felipe Contreras
2020-11-13  8:51                     ` Roman Perepelitsa
2020-11-14  0:52                       ` Felipe Contreras
2020-11-14  5:41                         ` Roman Perepelitsa
2020-11-16 19:41                           ` Felipe Contreras
2020-11-16 20:22                             ` Roman Perepelitsa
2020-11-17 20:28                               ` Felipe Contreras
2020-11-18 22:45                                 ` Daniel Shahaf
2020-11-22  1:20                                   ` Felipe Contreras
2020-11-23  4:00                                     ` Daniel Shahaf
2020-11-23  6:18                                       ` Felipe Contreras
2020-11-19  2:59                                 ` Bart Schaefer
2020-11-22  1:50                                   ` Felipe Contreras
2020-11-17 20:54                             ` Bart Schaefer
2020-11-22  1:49                               ` Felipe Contreras
2020-11-23  6:48                                 ` Bart Schaefer
2020-11-23  7:26                                   ` Felipe Contreras
2020-11-23 20:26                                     ` Bart Schaefer
2020-11-23 23:39                                       ` Felipe Contreras
2020-11-24  0:52                                         ` Bart Schaefer
2020-11-25  8:46                                           ` Felipe Contreras
2020-11-27 15:44                                             ` Daniel Shahaf
2020-11-27 20:49                                               ` Felipe Contreras
2020-11-27 20:59                                                 ` Daniel Shahaf
2020-11-27 21:33                                                   ` Bart Schaefer
2020-11-27 23:37                                                     ` Daniel Shahaf
2020-11-27 23:45                                                       ` Bart Schaefer
2020-11-28  0:24                                                       ` Bart Schaefer
2020-11-28  7:32                                                         ` Bart Schaefer
2020-11-28 12:05                                                         ` Felipe Contreras
2020-11-12 19:26                 ` Bart Schaefer
2020-11-12 21:48                   ` Felipe Contreras
2020-11-13 22:17                     ` Bart Schaefer
2020-11-14  0:58                       ` Felipe Contreras
2020-11-11 18:36         ` Bart Schaefer
2020-11-11 21:08           ` Felipe Contreras
2020-11-11 17:02     ` Peter Stephenson
2020-11-11 18:05       ` Felipe Contreras

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAMP44s29X6VBRwpwVj-anXax0UU6WRnukmUqrpoweM_cqbDdTQ@mail.gmail.com \
    --to=felipe.contreras@gmail.com \
    --cc=roman.perepelitsa@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).