Issue
Let's say I have:
function x {
echo "x"
}
call_func="x"
Now, I can simply use eval
as follows:
eval $call_func
but I was wondering if there was some other way of invoking the function (if it exists) whose name is stored in the variable: call_func
.
Solution
You should be able to just call the function directly using
$call_func
For everything else check out that answer: https://stackoverflow.com/a/17529221/3236102 It's not directly what you need, but it shows a lot of different ways of how to call commands / functions.
Letting the user execute any arbitrary code is bad practice though, since it can be quite dangerous. What would be better is to do it like this:
if [ $userinput == "some_command" ];then
some_command
fi
This way, the user can only execute the commands that you want them to and can even output an error message if the input was incorrect.
Answered By - Dakkaron Answer Checked By - Terry (WPSolving Volunteer)