Shell variable doesn't change with pipe-while-read combination

Solution 1:

In your first case, all commands executed in the same shell. The content of COUNT is changed.

In your second case a subshell is started with piping |, and changes in the subshell have no effect in the current shell. But he subshell knows the variable COUNT and the first output is 2.