I was trying to understand how "while read" works in bash, and i came accross a behaviour that i can't explain:
root@antec:/# var=old_value
root@antec:/# echo "new_value" | read var && echo $var
old_value
root@antec:/#
It works fine with "while read":
root@antec:/# var=old_value
root@antec:/# echo "new_value" | while read var; do echo $var; done
new_value
root@antec:/#
Can someone explain why it does not work when read is used without while ? Moreover, i don't understand how the value of "var" in the main shell can be seen from the allegedly subshell after the pipe ..
Thank you
I believe this is precedence problem, with | having higher precedence than &&. First is grouped as:
(echo "new_value" | read var) && echo $var
Second is grouped as:
echo "new_value" | (while read var; do echo $var; done)
Why do you think it works in the second case? It doesn't really update var. do echo $var after the line with while and see.
Explanation: whatever comes after | is executed in a subshell that has its own copy of var. The original var is not affected in either case. What is echoed depends on whether echo is called in the same subshell that does read or not.
I didn't want to print the variable immediately but use it later on and this works:
var=old_value
read var < <(echo "new_value")
echo $var
> new_value
alternatively:
var=old_value
tmp=$(echo "new_value")
read var <<<"$tmp"
echo $var
> new_value
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