As far as I know, while loops in shell are executed in a sub-shell, so they cannot modify variables outside the loop.
I'm writing a shell script and I want to store all the internal IPs of my machine into a single variable and so process this variable with a for loop to filter them one by one with iptables.
I could write this piece of code:
ip route show default | awk '{ print $5 }' | while read line; do
ip address show dev ${line} scope global | awk '/inet / {sub(/\/.*/, "", $2); print $2}' | while read line; do
echo "${line} "
done
done
Output:
10.17.0.49
192.168.1.4
My question is:
How can I store all these lines emitted by a while loop into a single variable (as while loop variables are volatile)?