Bash

將一組 bash 命令傳遞給 bash 函式

  • July 21, 2019

我想傳遞以下一組 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

使用數組怎麼樣?

#! /bin/bash
myeval () {
   for command in "$@" ; do
       $command
   done | other_func
}

myeval 'echo Apple' 'echo Banana'

引用自:https://unix.stackexchange.com/questions/531292