dpkg --list |grep linux-image |grep "ii " | while read line
do
arr=(${line})
let i=i+1
_constr+="${arr[2]} "
done
echo $i
echo ${_constr}
The echo statements outside of the loop do not display the expected variables.
How should I make the contents of the variable propagate outside the loop?
The problem is the pipe, not the loop. Try it this way
Pipes are executed in a subshell, as noted by Blagovest in his comment. Using process substitution instead (this is the
< <(commands)syntax) keeps everything in the same process, so changes to global variables are possible.Incidentally, your pipeline could be improved as well
One less invocation of
grepto worry about.