![将一组 bash 命令传递给 bash 函数](https://linux22.com/image/157563/%E5%B0%86%E4%B8%80%E7%BB%84%20bash%20%E5%91%BD%E4%BB%A4%E4%BC%A0%E9%80%92%E7%BB%99%20bash%20%E5%87%BD%E6%95%B0.png)
我想传递以下一组 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'