Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
zsh adds empty string to local parameter when += is used
- X-seq: zsh-workers 47850
- From: jamil bio <jamilbio20@xxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: zsh adds empty string to local parameter when += is used
- Date: Wed, 20 Jan 2021 13:16:54 -0300
- Archived-at: <https://zsh.org/workers/47850>
- Archived-at: <http://www.zsh.org/sympa/arcsearch_id/zsh-workers/2021-01/a7422e8c-48cd-23eb-8fa9-66a85f0a9ad5%40gmail.com>
- List-id: <zsh-workers.zsh.org>
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!
Messages sorted by:
Reverse Date,
Date,
Thread,
Author