Pass command as a function parameter

Hi guys,
can someome help with this question, I have defined a function that takes a command as a parameter, but when the command is executed from the function it will throw errors because what I believe is a special character escaping issue. I tried using the backslash to escape the pipe | and > signs but still got the same error

function execlocal {
        echo "Running CMD:\"$*\""
        $*
        STATUS=$?
}

CMD='ls -l | grep pl > out 2>&1'
execlocal $CMD

 
error: 
ls: \|: No such file or directory

can someome help please?
my shell is bash.

Thanks

Use eval to execute the command:

function execlocal {
        echo "Running CMD:\"$*\""
        eval $*
        STATUS=$?
}

CMD='ls -l | grep pl > out 2>&1'
execlocal $CMD
1 Like

Thank you very much.