On Tue, Mar 02, 2010 at 03:41:36PM +0530, Kalidas Yeturu wrote: > p=0; for lig in `cat a`; do cat b | while read i; do echo "printing $p"; > ((p=$p+1)); done; done > > #outputs (I DO NOT EXPECT p TO BE RESET EACH TIME WHILE RUNS) > printing 0 > printing 1 > printing 2 > printing 0 > printing 1 > printing 2
Each command of a pipeline is run in a separate process. This includes your while loop. The increment of p is done in a child process, so the value is lost each time you finish the inner while loop. In the example you gave, you can work around that by avoiding the "useless use of cat" and redirecting the file into the while loop instead. p=0 for lig in `cat a`; do while read i; do echo "printing $p" ((p=$p+1)) done < b done Unfortunately, most people use misleading examples when they are trying to describe a problem. Let's assume that you do not actually have a static input file; that "b" is in fact a stream generated by some process. The workaround for that case is to use "process substitution" (which is just a fancy automatic kind of named pipe). p=0 for lig in `cat a`; do while read i; do echo "printing $p" ((p=$p+1)) done < <(command-b) done See also http://mywiki.wooledge.org/BashFAQ/024