小编Duc*_*ack的帖子

将导出命令传递给 shell 脚本中的函数

#!/bin/sh

execute_cmd()
{
   $($@)
}

execute_cmd export MY_VAR=my_val
echo ${MY_VAR}
Run Code Online (Sandbox Code Playgroud)

由于$()在子 shell 中执行,$MY_VAR因此在脚本运行的 shell 中没有正确设置。

我的问题是,如何将导出命令传递给函数并在运行脚本的当前 shell 中执行它?

shell function

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

标签 统计

function ×1

shell ×1