Skip to main content

Pass a group of bash commands to bash function [Resolved]

I want to pass the following group of bash commands

{ echo Apple; echo Banana; }

as an argument to a bash function defined in .bashrc as follows:

BashFunction(){
"$@" | SomeOtherFunction
}
BashFunction '{ echo Apple; echo Banana; }'

But I get this error:

{ echo Apple; echo Banana; }: command not found

If I remove the quotes from the bash function

BashFunction(){
$@ | SomeOtherFunction
}

Then I get this error

{: command not found

Question Credit: Nikhil
Question Reference
Asked July 21, 2019
Posted Under: Unix Linux
28 views
1 Answers

What about using an array?

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

myeval 'echo Apple' 'echo Banana'

credit: choroba
Answered July 21, 2019
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA