bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: removing null elements from an array


From: Stephane Chazelas
Subject: Re: removing null elements from an array
Date: Fri, 18 Jul 2008 14:47:07 +0100
User-agent: Mutt/1.5.16 (2007-09-19)

On Fri, Jul 18, 2008 at 08:52:47AM -0400, Poor Yorick wrote:
> Stephane Chazelas wrote:
>>
>> In zsh, removing the empty elements is just a matter of
>>
>> var1=($var1)
>
> Wouldn't this corrupt the elements with spaces, just as in bash?
[...]

No, zsh doesn't do word splitting (nor filename generation) on
variable expansion unless you explicitely request it (or unless
called in sh or ksh compatibility).

In bash and ksh, $var1 expands to the list of files that match
the patterns resulting from the word splitting of the element of
indice 0 in the var1 array (and in scalar context to that
element without globbing nor splitting obviously). In zsh, it
expands to the non-empty elements of the array (and in scalar
context to the concatenation of those elements with the first
character of $IFS).

In zsh, string and arrays are of a different type. In ksh (and
bash in most situations), all variables are arrays, and $var is
a shortcut for ${var[0]}.

To perform word splitting in zsh, you have to use $=var ($=
looks like scissors), and to perform globbing: $~var. So bash's
address@hidden can be written address@hidden in zsh, but you can also make
zsh behave as bash by default with some option settings (like in
sh or ksh emulation).

Which makes me think that a way to answer the OP's request is to
do:

set -f; IFS=
var2=(address@hidden)

(which changes the indices).

-- 
St├ęphane




reply via email to

[Prev in Thread] Current Thread [Next in Thread]