zsh-workers
 help / color / mirror / code / Atom feed
* Inconsistent behavior of the test command with associative arrays
@ 2024-04-17 21:37 limited time
  2024-04-17 22:13 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: limited time @ 2024-04-17 21:37 UTC (permalink / raw)
  To: zsh-workers

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

% zsh -f
% typeset -A array=(key1 value1 key2 value2);
test $array[key1] ; echo $? ;
test $array[key2] ; echo $? ;
test $array[key3] ; echo $? ;
test $array[key4] ; echo $? ;
if test $array[key1] -a $array[key2] ; then
  echo "Condition evaluated to true" ;
fi                                             
if test $array[key3] -a $array[key4] ; then             
  echo "Condition evaluated to true" ;
fi
0
0
1
1
Condition evaluated to true
Condition evaluated to true

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Inconsistent behavior of the test command with associative arrays
  2024-04-17 21:37 Inconsistent behavior of the test command with associative arrays limited time
@ 2024-04-17 22:13 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2024-04-17 22:13 UTC (permalink / raw)
  To: limited time; +Cc: zsh-workers

[-- 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 --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-04-17 22:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-17 21:37 Inconsistent behavior of the test command with associative arrays limited time
2024-04-17 22:13 ` Bart Schaefer

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