我想传递以下一组 bash 命令
{ echo Apple; echo Banana; }
作为 bash 函数的参数,定义.bashrc
如下:
BashFunction(){
"$@" | SomeOtherFunction
}
BashFunction '{ echo Apple; echo Banana; }'
但我收到这个错误:
{ echo Apple; echo Banana; }: command not found
如果我从 bash 函数中删除引号
BashFunction(){
$@ | SomeOtherFunction
}
然后我得到这个错误
{: command not found
答案1
使用数组怎么样?
#! /bin/bash
myeval () {
for command in "$@" ; do
$command
done | other_func
}
myeval 'echo Apple' 'echo Banana'