父子shell脚本问题

Sac*_*iya 1 variables shell scope

我有两个脚本parent.sh和child.sh.parent.sh中有一个变量,需要子进程访问.我通过在父脚本中导出变量来实现这一点,并且变量可用于子进程?

是否有任何方法可以修改父shell中定义的导出变量的值?

Parent.sh

#!/bin/bash
export g_var=2
./child.sh
Run Code Online (Sandbox Code Playgroud)

child.sh

#!/bin/bash
g_var=`expr $g_var + 1 `   #This modification is available in child shell only.
Run Code Online (Sandbox Code Playgroud)

out*_*tis 5

大多数shell支持source内置函数,后者在当前shell中执行脚本而不是生成新脚本.在bash和其他一些命令中.,命令相当于source.我还没看过,但POSIX shell标准可能要求两者相同.尝试:

#!/bin/bash
export g_var=2
. ./child.sh
Run Code Online (Sandbox Code Playgroud)

"child.sh"与您的示例相同.