Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 4 Dec 2019 20:28:02 -0000 (UTC)
From:      Christian Weisgerber <naddy@mips.inka.de>
To:        freebsd-questions@freebsd.org
Subject:   Re: Counter in sh inside loop, value "encapsulation"
Message-ID:  <slrnqug5mi.srf.naddy@lorvorc.mips.inka.de>
References:  <20191204181300.8dd0e03c.freebsd@edvax.de>

next in thread | previous in thread | raw e-mail | index | archive | help
On 2019-12-04, Polytropon <freebsd@edvax.de> wrote:

> #!/bin/sh
> COUNT=0
> grep "^https" ${INFILE} | while read URL; do
> 	COUNT=`expr ${COUNT} + 1`
> done
> echo "URLs processed: ${COUNT}" # <--- (!) THIS IS ZERO!

Each command of a pipeline is executed in a subshell.
(Some shells, notably AT&T ksh, behave differently.)

This is the single most asked shell scripting question.

-- 
Christian "naddy" Weisgerber                          naddy@mips.inka.de



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?slrnqug5mi.srf.naddy>