简体   繁体   English

bash 进程替换 + 在主进程和子进程中使用相同的变量

[英]bash process substitution + using same variable in main and sub-process

If I do如果我做

x=42
IFS= read -r x < <(echo "$x")
echo "$x"

am I guaranteed to get back 42, or is there a race condition like for reading from / writing to the same file in a pipeline ( cat /tmp/x | grep -v pat | cat >/tmp/x )?我是否保证返回 42,或者是否存在竞争条件,例如读取/写入管道中的同一文件( cat /tmp/x | grep -v pat | cat >/tmp/x )?

OK, for read it's kind of obvious, what about mapfile -- might it clear the destination (= source for subprocess) array before forking off the substituted process?好的, read很明显, mapfile怎么样——它可能会在分叉替代进程之前清除目标(= 子进程的源)数组吗?

arr=( 'forty' 'two' )
mapfile -t arr < <(printf '%s\n' "${arr[@]}")
declare -p arr

Edit : maybe a better comparison would be to编辑:也许更好的比较是

{ echo 42; echo 13; } >/tmp/x
cat >/tmp/x < <(grep -v 13 /tmp/x)
cat /tmp/x

using same variable in main and sub-process在主进程和子进程中使用相同的变量

Each process has it's own space.每个进程都有自己的空间。 They are separate variables.它们是单独的变量。

am I guaranteed to get back 42我能保证找回来吗 42

Yes.是的。

might it clear the destination (= source for subprocess) array before forking off the substituted process?在分叉替代进程之前,它可能会清除目标(= 子进程的源)数组吗?

No.不。

The subprocess <(...) has a copy of all the parent environment.子进程<(...)拥有所有父环境的副本 It does not affect parent in any way.它不会以任何方式影响父母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM