在 Bash 中的函数内部使用声明

Ano*_*ous 3 bash shell global-variables

我想使用函数更改全局变量(或至少附加到它)。

input="Hello"
example=input    

func() {
    declare -x $example="${input} World"
}

func
echo $input
Run Code Online (Sandbox Code Playgroud)

其输出将是“Hello”的原始值。如果该函数能够更改原始值,我希望它。有没有其他方法可以完成此任务。请注意,我需要设置example=input然后执行示例(变量)的操作。

顺便说一句,如果我改用eval该函数,它会抱怨 World 是一个函数或其他东西。

Foo*_*Bah 6

您尝试过使用导出吗?

export $example="${input} World"
Run Code Online (Sandbox Code Playgroud)