Strange nested loop behavior in bash

I would like to know why this bash script

#!/bin/bash seq 1 3 > foo COUNT=0 while read VAR1; do while read VAR2; do let COUNT++ echo -n $COUNT done < foo done < foo 

outputs: 123456789

while this other bash script that (AFAIK) should do the same

 #!/bin/bash seq 1 3 > foo COUNT=0 while read VAR1; do cat foo | while read VAR2; do let COUNT++ echo $COUNT done done < foo 

outputs: 123123123

+4
source share
4 answers

The difference is that the conveyor runs in a subshell. Therefore, changes to the COUNT variable are not saved after the end of the inner loop in the second case.

+3
source

Using | , call a subshell, and therefore your variable does not contain a value in it.

A quick look at the debug log will explain the script. Run both bash -x scriptname as bash -x scriptname and you have to answer yourself.

+3
source

In the second example, the inner loop is in the pipeline. Therefore, it is executed in a subshell; therefore, changes to the COUNT variable are local to the inner loop.

+2
source

Your cat foo | ... cat foo | ... looks suspicious to me. Basically, I think you are returning the foo file over and over again without entering the VAR2 while .

0
source

Source: https://habr.com/ru/post/1483232/


All Articles