在不同 bash 脚本中共享变量
Sharing variables in different bash scripts
假设我有一个script1.sh
像
这样的脚本
#!/bin/bash
#do something
export THING="a"
source script2.sh & #so running in background
sleep 1 #or do something
export THING="b"
source script2.sh & #so running in background
因此导出变量 THING
,由 script2.sh
读取:
#!/bin/bash
echo "PRINT1: ${THING}"
sleep 5
echo "PRINT2: ${THING}"
假设 script2.sh
在调用时“冻结” THING
的值是否正确? (也就是说,即使 运行 在后台,它在执行过程中也不会改变)。
我对此进行了测试,似乎是这样,但我想检查一下这是否是预期的一般行为。
提前致谢,
SL
Is it correct to assume that script2.sh is "freezing" the value of THING at the moment of the call?
是的。该值未“冻结”,您可以更改它。这只是一个副本。
每个进程都有自己的环境。子进程在创建子进程时创建为当前环境的副本。
假设我有一个script1.sh
像
#!/bin/bash
#do something
export THING="a"
source script2.sh & #so running in background
sleep 1 #or do something
export THING="b"
source script2.sh & #so running in background
因此导出变量 THING
,由 script2.sh
读取:
#!/bin/bash
echo "PRINT1: ${THING}"
sleep 5
echo "PRINT2: ${THING}"
假设 script2.sh
在调用时“冻结” THING
的值是否正确? (也就是说,即使 运行 在后台,它在执行过程中也不会改变)。
我对此进行了测试,似乎是这样,但我想检查一下这是否是预期的一般行为。
提前致谢, SL
Is it correct to assume that script2.sh is "freezing" the value of THING at the moment of the call?
是的。该值未“冻结”,您可以更改它。这只是一个副本。
每个进程都有自己的环境。子进程在创建子进程时创建为当前环境的副本。