From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 2f89dfb2 for ; Sun, 31 Mar 2019 12:59:15 +0000 (UTC) Received: (qmail 21152 invoked by alias); 31 Mar 2019 12:58:57 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: List-Unsubscribe: X-Seq: 23894 Received: (qmail 19493 invoked by uid 1010); 31 Mar 2019 12:58:57 -0000 X-Qmail-Scanner-Diagnostics: from out3-smtp.messagingengine.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.101.1/25398. spamassassin: 3.4.2. Clear:RC:0(66.111.4.27):SA:0(-2.6/5.0):. Processed in 4.994186 secs); 31 Mar 2019 12:58:57 -0000 X-Envelope-From: d.s@daniel.shahaf.name X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: none (ns1.primenet.com.au: domain at daniel.shahaf.name does not designate permitted sender hosts) 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== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedutddrledvgdeitdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvuffkfhggtggugfgjfgesthektddttderjeenucfhrhhomhepffgrnhhi vghlucfuhhgrhhgrfhcuoegurdhssegurghnihgvlhdrshhhrghhrghfrdhnrghmvgeqne cukfhppeejledrudektddrheehrdeileenucfrrghrrghmpehmrghilhhfrhhomhepugdr shesuggrnhhivghlrdhshhgrhhgrfhdrnhgrmhgvnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Date: Sun, 31 Mar 2019 12:58:13 +0000 From: Daniel Shahaf To: Anssi Saari Cc: zsh-users@zsh.org Subject: Re: Confused about splitting Message-ID: <20190331125813.pd45aqx4bvdjnrgr@tarpaulin.shahaf.local2> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: NeoMutt/20170113 (1.7.2) 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