This is a side-effect of using while
in a pipeline. There are two workarounds:
1) put the while
loop and all the variables it uses in a separate scope as demonstrated by levislevis86
some | complicated | pipeline | {
while read line; do
foo=$( some calculation )
done
do_something_with $foo
}
# $foo not available here
2) if your shell allows it, use process substitution and you can redirect the output of your pipeline to the input of the while loop
while read line; do
foo=$( some calculation )}
done < <(some | complicated | pipeline)
do_something_with $foo
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…