Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is it possible to treat a block of commands as an anonymous function?

function wrap_this {
   run_something
   # Decide to run block or maybe not.
   run_something else
}

wrap_this {
   do_something
   do_somthing else
}

# Do something else

wrap_this {
   do_something_else_else
   do_something_else_else_else
}

(I realize you create a function or file for each block, but I find this option clearer and easier to read in certain situations.)

while does it with do/done and function does it with { multiple lines }. I realize BASH does not have anonymous functions, but is it possible to pass multiple commands to another function, like you can do when defining a function or while?

share|improve this question
    
Do you mean you want to decorate (in Python parlance) - i.e., return a function from a function? Your example, syntactically, isn't even BASH: is wrap_this supposed to be a function or a function call? –  Mel Boyce Jun 30 '13 at 9:52
    
It is unclear to me what you want to do. As Mel pointed out, what you have written even syntactically valid, but it is unclear to me how what you have written relates to anonymous functions. –  Chris Down Jun 30 '13 at 9:59

2 Answers 2

You can put code in a string and pass it to eval or sh or simply interpolate it.

perform () {
  "$@"
}

perform echo "moo"

You can quickly end up in deep quoting trouble, though.

share|improve this answer
1  
That should be either perform echo moo or eval "$@" in perform –  Stéphane Chazelas Jun 30 '13 at 17:08
    
You're entirely correct -- quoting fixed. Thanks! –  tripleee Jun 30 '13 at 17:31
    
Actually, I was looking for perform { echo "moo" \n echo "moo moo" \n echo "moo moo moo" }. I already knew you can pass one command. But, I'm looking for multiple lines, not just one command or one line. Thanks though for trying. –  dgo.a Jul 2 '13 at 10:41

No, bash doesn't have anonymous functions. It is however possible to pass a function name and arguments as strings and have bash call it.

function wrap() {
    do_before
    "$@"
    do_after
}

wrap do_something with_arguments

This is however, somewhat limited. Dealing with quoting can become a problem. Passing more than one command is also a complication.

share|improve this answer
    
Not a nightname, all you have to do is change $* to "$@" –  glenn jackman Jun 30 '13 at 11:39
    
Multi-line function bodies should work fine with this method. –  paraxor Jun 30 '13 at 12:24
    
Glenn, I forgot about that form, thanks. It doesn't completely solve the quoting problem though. Evan, a single command wrapped over multiple lines would work fine, I meant having more than one command in the body as per the examples in the question. I'll update my answer to address both comments. –  David Baggerman Jun 30 '13 at 21:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.