Counter in sh inside loop, value "encapsulation"

Christian Weisgerber naddy at mips.inka.de
Wed Dec 4 20:30:14 UTC 2019


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.

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


More information about the freebsd-questions mailing list