Ben Kim Ben Kim - 1 month ago 6
Bash Question

Shell Script call a function with a variable?

Hi I'm creating a shell script.

and an example code looks like

#!/bin/bash

test_func(){
{
echo "It works!"
}

funcion_name = "test_func"


I want to somehow be able to call test_func() using the variable "function_name"

I know that's possible in php using call_user_func($function_name) or by sying $function_name()

is this also possible in the shell scripting?

Huge appreciation for the help! :)

Answer
#!/bin/bash
test_func() {
    echo "It works!"
}

function_name="test_func"

eval ${function_name}
Comments