Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Confused about splitting
- X-seq: zsh-users 23894
- From: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
- To: Anssi Saari <as@xxxxxx>
- Subject: Re: Confused about splitting
- Date: Sun, 31 Mar 2019 12:58:13 +0000
- Cc: zsh-users@xxxxxxx
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= daniel.shahaf.name; h=date:from:to:cc:subject:message-id :references:mime-version:content-type:content-transfer-encoding :in-reply-to; s=fm2; bh=YMK5jVdqXY6CFeginwmaYQ10/1X2CVVwXdFn3BpR ghY=; b=1oERratBE37OPDbFOYM7EPyNsplF7GaQlc/PfacbnHFZupHbPVlM3ue0 T5psJcyuXR1vu/fl3qPy3oVRmxbUv8g1H/l8VEeNPgoiCcu0W5dyQPgi6bJ7gr96 ajBZtuHsMBIU6FwFE94ecMIeHOS67Pbn0QGtrVBfYmXKPjxJG5mlf48KSSsI7wZg HfTK4Z+Z2fSLubYdDhEgUYNjqbrRsMhL0E2DHjgCOgcFmMIaC/L2Rh1wlnmn32By RvTbzAP6yefAI7Hl0gCr2FRPSE0U0vXhVms8SSwmG1cQWfO+J1XRFBApsRozxt+K lkMt8oJ5KMik8atvm9p5x39bKxpoPg==
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=YMK5jVdqXY6CFeginwmaYQ10/1X2CVVwXdFn3BpRg hY=; b=Dq0fmPYnb6896ELGUue8XDNrB3wm3NC1OWTiKr3zf1vadG6/rcxGURa18 nf9Ibzi8sfEjp95zqpj9siK95vvSgUvDd3X9tExnAfqXjxPiWaF97aYCv1Fasdz7 aQVkMxplq8k7dNq2fRNbsWrFUPRB/foSDVvMhcqI9lpKoXfEOGfPlAPXh1NC0COb GVuU5JJBFiVmB5ztwY+4ZsG9yEo6YWw0b0ftte0wqW6pm2SaKtIl9KKJhgFsnBXT 2Dt1OnVxRB+xIRhqINlq9+dPo+BAm3DMP/FcUgAELF3hUmLKWuSsQrLkK16jBEdE /TO1WciLY4IDQDMIcTDRWytzKLKWg==
- In-reply-to: <vg3k1gf4dmf.fsf@coffee.modeemi.fi>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <vg3k1gf4dmf.fsf@coffee.modeemi.fi>
Anssi Saari wrote on Sun, Mar 31, 2019 at 12:06:16 +0300:
> foofunc2 () {
⋮
> setopt shwordsplit
This should have been «setopt localoptions shwordsplit».
⋮
> }
> But what if I don't want to set shwordsplit? I came up with
>
> foofunc3 () {
> if [[ $# -ge 1 ]]
> then
> if=$1
> else
> if=eno1
> fi
> while read g
> do
> if [[ $g =~ $if ]]
> then
> first=${=g}
> break
> fi
> done < /proc/net/dev
>
> echo $first
> echo length of first is $#first
> }
>
> But this splits each character into an array element. I don't understand
> this behavior at all. As I understand it, = in the assignment to $first
> is supposed to turn shwordsplit on temporarily but it really doesn't
> seem to. IFS is not set so field separator should be default, meaning
> whitespace.
«${=g}» does split the value of the parameter 'g'; you can see that with
«printf '[%s]' ${=g}». Then, that intermediate result — an array of words — is
assigned to the parameter 'first' scalarly, so the value of $first is set to be
the words of the array joined by spaces as a single string. That's why «echo
$first» has the right output. The «echo $#first» line prints the length of the
string "$first" in characters, because that's what the «$#foo» syntax does when
foo is a scalar.
The fix is to add back the parentheses to make the assignment an array
assignment: «first=( ${=g} )».
Furthermore, note that if $first had been an array of one-character strings,
the output of the first 'echo' would have been incorrect: compare «echo $foo»
(echos a string) and «echo ${(s::)foo}» (echos an array of one-character
strings).
Cheers,
Daniel
Messages sorted by:
Reverse Date,
Date,
Thread,
Author