相关疑难解决方法(0)

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

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

input="Hello"
example=input    

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

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

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

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

bash shell global-variables

3
推荐指数
1
解决办法
1609
查看次数

标签 统计

bash ×1

global-variables ×1

shell ×1