获取在eval命令中声明的变量

时间:2020-04-18 18:17:07

标签: bash shell sh

我想使用初始化为下标的变量,以避免用echoreturn返回它。

例如,我的目标是使用以下脚本输出$ myvar:

eval "./script.sh"
echo -n $myvar

在script.sh中这样声明:

declare -gx myvar # global and exported var
myvar=42

输出:

没事

是否还有其他命令或标志要添加到declare -gx的替换中?还是在eval之前将其移动是解决方案?

1 个答案:

答案 0 :(得分:0)

您可以使用source代替eval:

source "./script.sh"