zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: Oliver Kiddle <okiddle@yahoo.co.uk>
Cc: zsh-workers@zsh.org
Subject: Re: PATCH: [[ -v varname ]]
Date: Thu, 15 Sep 2016 12:08:55 +0100	[thread overview]
Message-ID: <20160915110854.GA10122@chaz.gmail.com> (raw)
In-Reply-To: <90414.1473890510@hydra.kiddle.eu>

2016-09-15 00:01:50 +0200, Oliver Kiddle:
> On 9 Sep, Stephane Chazelas wrote:
> > You guys may want to comment on the latest proposal there:
> > http://austingroupbugs.net/file_download.php?file_id=31&type=bug
> > as it would make zsh non-conformant.
> 
> One thing it also includes is the bash/ksh [[ -v ... ]] condition for
> checking if a particular variable is set. Zsh has the ${+varname} syntax
> which provides the same functionality in a different form. The following
> patch adds the ksh form for compatibility.
[...]

Note that there's a lot of variation between the ksh93 and bash
implementation of [[ -v ]] (though maybe not so much on the part
covered by POSIX), for instance on variables that have been
declared (or exported or marked readonly) with different types
but not assigned to (at least in zsh, declared/exported
variables are always given a default value), on arrays or hashs
that have no element (or no element of indice 0 or "0"; for a
hash in bash [[ -v h ]] returns true iff h[0] is set and in ksh
never ([[ -v h[@] ]] doesn't work either)).

Also note that in ksh/bash, it's only for variables, not other
types of parameters ([[ -v 0 ]], [[ -v "#" ]] return false).

See also:

$ a='b[0`uname>&2`]' bash -c '[[ -v $a ]]'
Linux

$ bash -c 'typeset -n a=b; [[ -v a ]]'; echo $?
1
$ b= bash -c 'typeset -n a=b; [[ -v a ]]'; echo $?
0

Spurious error message:

$ ksh -c 'typeset -T A=(a=a); A a; [[ -v a.x ]]'
ksh: x: is not an element of a

a.x returns false if $a has a x but that's a discipline (or
array/hash with 0 element not set).


In any case, it looks like your patch does the sensible thing in
all the cases I've tried except that [[ -v @ ]] returns false
(while [[ -v * ]] return true). No big deal as nobody would ever
want to do that.

What to do in zsh (where scalar and arrays are distinct types) for 

zsh -c 'a=1234; [[ -v a[1] ]]

is up to debate (currently returns false).

See also
https://unix.stackexchange.com/questions/212183/how-do-i-check-if-a-variable-exists-in-an-if-statement/246703#246703

-- 
Stephane


  parent reply	other threads:[~2016-09-15 13:52 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-05 21:27 [PATCH] Use == in expressions instead of the deprecated = Teubel György
2016-09-08  8:35 ` Peter Stephenson
2016-09-08 11:16   ` Peter Stephenson
2016-09-08 14:31     ` Stephane Chazelas
2016-09-08 15:06       ` Stephane Chazelas
2016-09-08 15:14       ` Peter Stephenson
2016-09-08 16:39         ` Mikael Magnusson
2016-09-08 18:47           ` Stephane Chazelas
2016-09-09 12:03         ` Vincent Lefevre
     [not found]           ` <CGME20160912143133eucas1p11a4e6916dd70b3ceaa163bf3bddfb007@eucas1p1.samsung.com>
2016-09-12 14:31             ` Peter Stephenson
2016-09-09  8:52       ` Stephane Chazelas
2016-09-09  9:31         ` Peter Stephenson
2016-09-09 16:00           ` Stephane Chazelas
2016-09-14 22:01         ` PATCH: [[ -v varname ]] Oliver Kiddle
2016-09-15 10:55           ` Vincent Lefevre
2016-09-16  0:36             ` Bart Schaefer
2016-09-15 11:08           ` Stephane Chazelas [this message]
2016-09-15 11:22             ` wrong "export -p" output for exported but not set variables (Was: PATCH: [[ -v varname ]]) Stephane Chazelas
2016-09-09 17:01       ` [PATCH] Use == in expressions instead of the deprecated = Christian Neukirchen
2016-09-09 18:54         ` Stephane Chazelas
2016-09-09 13:05     ` Peter Stephenson

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=20160915110854.GA10122@chaz.gmail.com \
    --to=stephane.chazelas@gmail.com \
    --cc=okiddle@yahoo.co.uk \
    --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).