Can someone explain why this script doesn't produce the output I was expecting? #!/bin/bash #var=0ls -1 /tmp| while read filedo     echo $file     var=1doneecho var is $varI get a list of files followed by var is 0Why isn't var equal to 1? Is it because the while loop spawns a sub-shell?
Does while spawn a subshell in bash?
bash
Piping does. You can check for yourself, for example by printing $BASHPID from inside and outside of the while loop or by doing something like:ls | while read file; do    sleep 100;done, stopping it with C-Z and checking ps or ps --forest afterwards to see the process tree in your terminal session.You can avoid the subshell by piping a little differently:var=0while read filedo   echo $file; var=1done < <(ls -1 /tmp/)echo $var #=> 1