您好我正在创建一个shell脚本。
示例代码如
#!/bin/bash
test_func(){
{
echo "It works!"
}
funcion_name = "test_func"
我想以某种方式能够使用变量“function_name”
调用test_func()我知道在php中使用call_user_func($ function_name)或sying $ function_name()
是可能的这也可以在shell脚本中使用吗?
非常感谢您的帮助! :)
答案 0 :(得分: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
答案 1 :(得分:3)
#!/bin/bash
test_func() {
echo "It works!"
}
function_name="test_func"
eval ${function_name}