Counter in sh inside loop, value "encapsulation"

RW rwmaillists at googlemail.com
Thu Dec 5 13:35:27 UTC 2019


On Wed, 4 Dec 2019 20:28:02 -0000 (UTC)
Christian Weisgerber wrote:

> On 2019-12-04, Polytropon <freebsd at 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.

IIRC when this sort of thing came once before someone posted a way of
processing the output of a pipeline and having multiple variables
directly accessible to the main script. 

It didn't involve creating files or sockets, or storing the output of
the pipeline in a single variable. There was some trick that avoided
last stage being done in sub-shell.

Unfortunately, I forgot to make a note of it and I haven't been able to
find the thread again. Anyone know what it might be?


More information about the freebsd-questions mailing list