zsh-workers
 help / color / mirror / code / Atom feed
From: jamil bio <jamilbio20@gmail.com>
To: zsh-workers@zsh.org
Subject: zsh adds empty string to local parameter when += is used
Date: Wed, 20 Jan 2021 13:16:54 -0300	[thread overview]
Message-ID: <a7422e8c-48cd-23eb-8fa9-66a85f0a9ad5@gmail.com> (raw)

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

Hello good people,

It seems there is a bug which I will describe below.
I checked with usenet comp.unix.shell people and they said that is 
strange behavior.

I am using Z-shell 5.8. The problem is when I add elements to an array 
with += inside a function, an empty element is generated. The empty 
element is added only if I set array $files as local.

The following is an example code.


#!/bin/zsh
#!/bin/bash

func()
{
     local REPLY  files

     #unset files
     for REPLY in file1 file2 file3
     do
         files+=( "$REPLY" )
         echo "${#files[@]} -- ${files[@]}"
     done
}

func


If I set $files to local but unset it before the loop, the empty element 
is not added. Running the script with zsh returns:


2 --  file1
3 --  file1 file2
4 --  file1 file2 file3


While if we run the code with bash:


1 -- file1
2 -- file1 file2
3 -- file1 file2 file3


I was actually using a while read loop to read filenames, but the 
problem also occurs when I use the for loop.

So the workaround suggested is to set $files as local and then later 
define files=( ) which will prevent empty element being added somehow.

Thanks for all your efforts in making zsh so great!

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

             reply	other threads:[~2021-01-20 16:17 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-20 16:16 jamil bio [this message]
2021-01-20 16:22 ` Roman Perepelitsa
2021-01-20 16:26 ` Peter Stephenson
2021-01-20 16:29   ` Roman Perepelitsa
2021-01-20 16:31 ` Daniel Shahaf
2021-01-20 16:40   ` jamil bio

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=a7422e8c-48cd-23eb-8fa9-66a85f0a9ad5@gmail.com \
    --to=jamilbio20@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).