Ben*_*Kim 6 variables shell function call
嗨,我正在创建一个shell脚本.
并且示例代码看起来像
#!/bin/bash
test_func(){
{
echo "It works!"
}
funcion_name = "test_func"
Run Code Online (Sandbox Code Playgroud)
我想以某种方式能够使用变量"function_name"调用test_func()
我知道在php中使用call_user_func($ function_name)或sying $ function_name()是可能的
这也可以在shell脚本中使用吗?
非常感谢您的帮助!:)
e.d*_*dan 10
你想要内置的bash eval.来自man bash:
eval [arg ...]
The args are read and concatenated together into a single command. This command is then read and executed by the shell, and its exit status is returned as the value of eval. If there are no
args, or only null arguments, eval returns 0.
您也可以使用简单的变量替换来完成它,如
#!/bin/bash
test_func() {
echo "It works!"
}
function_name="test_func"
$function_name
Run Code Online (Sandbox Code Playgroud)
#!/bin/bash
test_func() {
echo "It works!"
}
function_name="test_func"
eval ${function_name}
Run Code Online (Sandbox Code Playgroud)