I could replicate the problem with various shells under FreeBSD, GNU/Linux, and Solaris. It had me head-scratching for more than an hour, so I decided to post the question here.
Due to the piping the read
is executed in its own subshell.
echo foo | while read a; do echo $a; done
will do what you expect it to.
alternative:
echo foo | (read a ; echo $a)
Edit:
If you need $a outside the subshell, you have to reverse the commands:
read a < <(echo foo); echo $a
this way the read is executed in the current process
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With