zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: limited time <burnermail4now@gmail.com>
Cc: zsh-workers@zsh.org
Subject: Re: Inconsistent behavior of the test command with associative arrays
Date: Wed, 17 Apr 2024 15:13:31 -0700	[thread overview]
Message-ID: <CAH+w=7Yx5qfwrGYzC_p=dP=1fZsWOXZrM1a-adVCieEjK1vdyg@mail.gmail.com> (raw)
In-Reply-To: <143C3C91-0198-4D62-8B81-9830CA7DB649@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1058 bytes --]

This doesn't have anything to do with associative arrays and everything to
do with "test" and quoting.

On Wed, Apr 17, 2024 at 2:37 PM limited time <burnermail4now@gmail.com>
wrote:

> % zsh -f
> % typeset -A array=(key1 value1 key2 value2);
> test $array[key1] ; echo $? ;
> test $array[key2] ; echo $? ;
>

These two are "test somestring" which is defined as true.


> test $array[key3] ; echo $? ;
> test $array[key4] ; echo $? ;
>

These two are just "test" (with no arguments at all, because key3 and key4
have no value) which is defined as false.


> if test $array[key1] -a $array[key2] ; then
>   echo "Condition evaluated to true" ;
> fi
>

This is "test string -a string" which is true because neither string is
empty.


> if test $array[key3] -a $array[key4] ; then
>   echo "Condition evaluated to true" ;
> fi
>

This is "test -a" because both expansions are empty and not quoted so they
disappear from the command line entirely.

By definition, "test -a" is treated as "test somestring" and is true.

[-- Attachment #2: Type: text/html, Size: 2549 bytes --]

      reply	other threads:[~2024-04-17 22:14 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-17 21:37 limited time
2024-04-17 22:13 ` Bart Schaefer [this message]

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='CAH+w=7Yx5qfwrGYzC_p=dP=1fZsWOXZrM1a-adVCieEjK1vdyg@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=burnermail4now@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).