Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: zsh adds empty string to local parameter when += is used
- X-seq: zsh-workers 47852
- From: Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
- To: jamil bio <jamilbio20@xxxxxxxxx>, zsh-workers@xxxxxxx
- Subject: Re: zsh adds empty string to local parameter when += is used
- Date: Wed, 20 Jan 2021 16:26:03 +0000 (GMT)
- Archived-at: <https://zsh.org/workers/47852>
- Archived-at: <http://www.zsh.org/sympa/arcsearch_id/zsh-workers/2021-01/1204351835.3138771.1611159963588%40mail2.virginmedia.com>
- Importance: Medium
- In-reply-to: <a7422e8c-48cd-23eb-8fa9-66a85f0a9ad5@gmail.com>
- List-id: <zsh-workers.zsh.org>
- References: <a7422e8c-48cd-23eb-8fa9-66a85f0a9ad5@gmail.com>
> On 20 January 2021 at 16:16 jamil bio <jamilbio20@xxxxxxxxx> wrote:
> func()
> {
> local REPLY files
>
> #unset files
> for REPLY in file1 file2 file3
> do
> files+=( "$REPLY" )
> echo "${#files[@]} -- ${files[@]}"
> done
> }
>
> func
>
> 2 -- file1
> 3 -- file1 file2
> 4 -- file1 file2 file3
Yes, this is a bit weird, but it's explicable.
"local files" makes the variable "files" into a scalar, so if it's ever
referred to it will return an empty string.
The first time the function executes
files+-( "$REPLY" )
it retrieves the existing $files. At this point that's just an empty
string. Then it adds the new element and assigns back to files. It's
only at this point that files becomes an array.
You can fix the problem by declaring files as an array,
local -a files
pws
Messages sorted by:
Reverse Date,
Date,
Thread,
Author