When I change to Korn shell by using #!/bin/ksh instead script works as expected and I see result as
Found
1
Why is that? does it mean under bash the scope of cnt variable outside while loop is not the same as cnt inside the wihile loop?
Just in case, the bash is:
bash --version
GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
It's not a question of "scope" exactly, code running behind a pipe runs in a completely different process. It gets a copy of the current values of the parent's variables when it's created. Changes aren't shared.
KSH and everything-except-KSH consider pipes in the opposite direction:
The current POSIX standards specify arithmetic expansions:
cnt=$(($cnt + 1))
and
cnt=$((cnt + 1))
They do not specify cnt=$[ $cnt + 1 ] .
There is a proposal to include ((cnt++)) in the next revision of the POSIX standards, but it isn't required yet.