我需要传递一个函数作为参数在Bash。例如,下面的代码:
function x() { echo "Hello world" } function around() { echo "before" eval $1 echo "after" } around x
应输出:
before Hello world after
我知道eval在这种情况下是不正确的,但这只是一个例子:)
任何想法?
如果你不需要任何像延迟函数名或其参数的计算的东西,你不需要eval:
原文链接:https://www.f2er.com/bash/390967.htmlfunction x() { echo "Hello world"; } function around() { echo before; $1; echo after; } around x
做你想要的。你甚至可以这样传递函数及其参数:
function x() { echo "x(): Passed $1 and $2"; } function around() { echo before; "$@"; echo after; } around x 1st 2nd
打印
before x(): Passed 1st and 2nd after