Bash pipeline prevents global variable assignment

unset v
function f {
  v=1
}
f | cat
echo v=$v
f 
echo v=$v

Why doesn't the pipeline (to any team) allow the first echo command to print 1? Second Echo Printing 1. I am using a bash shell. I see this by copying / pasting or running it as a script.

+5
source share
1 answer

All pipeline components (if more than one) are executed in a subshell, and their variable assignments are not stored in the main shell.

The reason for this is that bash does not support real multithreading (with simultaneous access to variables), only subprocesses that work in parallel.


How to avoid this :

, bash ( ). bash , :

f > >( cat )

, , . (, -?)

+9

All Articles