/ / Shell Script llama a una función con una variable? - función, shell, variables, llamada

Shell Script llama a una función con una variable? - función, shell, variables, llamada

Hola, estoy creando un script de shell.

y se ve un código de ejemplo

#!/bin/bash

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

funcion_name = "test_func"

Quiero de alguna manera poder llamar a test_func () usando la variable "nombre_función"

Sé que es posible en php usando call_user_func ($ function_name) o mordiendo $ function_name ()

¿esto también es posible en el script de shell?

Enorme agradecimiento por la ayuda! :)

Respuestas

3 para la respuesta № 1
#!/bin/bash
test_func() {
echo "It works!"
}

function_name="test_func"

eval ${function_name}

10 para la respuesta № 2

Quieres la fiesta incorporada eval. Desde 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.

También puede lograrlo con una simple sustitución de variables, como en

#!/bin/bash

test_func() {
echo "It works!"
}

function_name="test_func"

$function_name