Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Bug with unset variables
- X-seq: zsh-workers 47533
- From: Felipe Contreras <felipe.contreras@xxxxxxxxx>
- To: Roman Perepelitsa <roman.perepelitsa@xxxxxxxxx>
- Subject: Re: Bug with unset variables
- Date: Wed, 11 Nov 2020 10:56:08 -0600
- Archived-at: <https://zsh.org/workers/47533>
- Archived-at: <http://www.zsh.org/sympa/arcsearch_id/zsh-workers/2020-11/CAMP44s29X6VBRwpwVj-anXax0UU6WRnukmUqrpoweM_cqbDdTQ%40mail.gmail.com>
- Authentication-results: zsh.org; iprev=pass (mail-wm1-f47.google.com) smtp.remote-ip=209.85.128.47; dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none
- Cc: Zsh hackers list <zsh-workers@xxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=t2qUpKeiBwWgXTkWZcMpY9+NahZYr8HBXa6mFf3sBYQ=; b=P3l3Jf+g7LKFXGrYHSn9lwd8GYSQ9MZ2A4s6NdDF3cCgfPBXt1DBfkVjehDfXrssXZ dnMvFLHJho9FeFYLLZA0wcoS9TD7s1WjZ/tjX0QGh6pjk6eF7XXucxBCbr7adcn5Snmw osEW4mfNOWquQs1J8tmzcpNzz1QCA/SK457yeKJkGcBn/xgHcrXJfokJaoHe6FlKLqqE PVge29aHfPcdYylij5RD6K6wkLS7GD8+7dq4hXUFj1Ou8WW13RQNuz8Qh/yWFD3awCi0 I2bdJ6MGkC5nsiJxGstTiXJm6ZtNOCW4Yc1Zo+Ulv5vQ+2L6VDEkrmICpz8oT6MbyuqJ ll+A==
- In-reply-to: <CAN=4vMrvB--NSrRJEyUaaxhyErgyF4=+0da+=TE2mU_3KXopyA@mail.gmail.com>
- List-archive: <http://www.zsh.org/sympa/arc/zsh-workers>
- List-help: <mailto:sympa@zsh.org?subject=help>
- List-id: <zsh-workers.zsh.org>
- List-owner: <mailto:zsh-workers-request@zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-subscribe: <mailto:sympa@zsh.org?subject=subscribe%20zsh-workers>
- List-unsubscribe: <mailto:sympa@zsh.org?subject=unsubscribe%20zsh-workers>
- References: <CAMP44s2x20VKt0Kp3OuUEDVhE+7EL5yVOpCfkKnKr7opRu1v4A@mail.gmail.com> <CAN=4vMrvB--NSrRJEyUaaxhyErgyF4=+0da+=TE2mU_3KXopyA@mail.gmail.com>
- Sender: zsh-workers-request@xxxxxxx
On Wed, Nov 11, 2020 at 10:13 AM Roman Perepelitsa
<roman.perepelitsa@xxxxxxxxx> wrote:
>
> 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: ''".
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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author