I set variables in a loop. Why do they suddenly disappear after the loop terminates? Or, why can't I pipe data to read?

The problem

In most shells, each command of a pipeline is executed in a separate SubShell.

    # Non-working example (except in ksh88/ksh93)
    linecnt=0
    printf "%s\n" foo bar  | while read -r line
    do
        linecnt=$((linecnt+1))
    done
    echo "total number of lines: $linecnt" # prints 0

    # the problem also occurs without a loop
    var=0
    echo 2 | read -r var
    echo $var # also prints 0

The reason for this surprising behaviour is that a while/for/until loop runs in a SubShell when it's part of a pipeline. For the while loop above, a new subshell with its own copy of the variable linecnt is created (initial value, taken from the parent shell: "0"). This copy then is used for counting. When the while loop is finished, the subshell copy is discarded, and the original variable linecnt of the parent (whose value has not changed) is used in the echo command.

Different shells behave differently when using redirection or pipes with a loop:

Workarounds

Several possibilities to avoid the subshell exists:

* If the input is a file, remove the useless use of cat

* Group the commands and do it all in the subshell

* Use process substitution (BASH only)

* Use a named pipe (POSIX)

* Use a coprocess (ksh, even pdksh, oksh, mksh..)

* Another useful trick (using Bash/ksh93 syntax) is breaking a variable into words using read: