On Thursday, January 26, 2012 11:39:47 PM Peng Yu wrote:
> Hi,
>
> I'm wondering if there is any concept like closure in bash.
Bash functions (and most shells with locals) always are scoped based upon
where they're called. There's no way of emulating closures and continuations
short of saving the entire environment and then sourcing it later (probably in
a subshell). Given how limited functions are to begin with in terms of
"passability", I can't think of many cases where it would be useful without
modifying a whole lot of other things about how functions work.
An exported function doesn't come packaged with the environment in which it
was declared or exported. Exported functions are really only useful to avoid
having to redefine them in child shells. I mostly use them for testing small
things and almost never in a useful script.
Newfangled ksh93 user-defined types probably give you some function-object-like
behaviors you might use. I haven't tested them extensively.
--
Dan Douglas
signature.asc Description: This is a digitally signed message part.