Local variables after loop exit

前端 未结 5 651
眼角桃花
眼角桃花 2021-01-03 13:50

I am having some issues with local variables after exiting a loop. The variable max ends up with the value 0, despite the code below:

max=0
cat          


        
5条回答
  •  一生所求
    2021-01-03 14:09

    The pipe before your while is putting everything inside the loop into a separate shell, and thus a separate identifier context (essentially a new environment).

    Redirecting the tmp file into the while loop via < will keep your loop and variables all in the same execution context.

    while read line
    do
        # your loop stuff 
    done < tmp
    

提交回复
热议问题