bash background process modify global variable

后端 未结 3 425
陌清茗
陌清茗 2020-12-03 06:45

I have a global var foo=\"some value\" and a background process back_func, I want to the background process to access $foo and modify its value, which can be seen by the mai

3条回答
  •  北海茫月
    2020-12-03 07:34

    According to the Bash manual here,

    If a command is terminated by the control operator ‘&’, the shell executes the command asynchronously in a subshell.

    And since a process run in a subshell cannot modify the environment of the parent shell, I guess what you are trying to do is only possible via temp files / named pipes. Or you could rethink your approach.

提交回复
热议问题