[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Closure concept in bash

From: Greg Wooledge
Subject: Re: [Help-bash] Closure concept in bash
Date: Fri, 27 Jan 2012 08:35:39 -0500
User-agent: Mutt/

On Thu, Jan 26, 2012 at 11:39:47PM -0600, Peng Yu wrote:
> I'm wondering if there is any concept like closure in bash. In the
> first example, when export -f testing, the parallel command work just
> fine. But in the second example, I just exported testing2, var is not
> exported.  Then the parallel doesn't work as expected.

What are you trying to do?  What is "expected"?

> Does anybody know if there is something like closure to eliminated the
> need of extra exports? Thanks!

What do you mean by "closure"?  Apparently you do not mean Kleene Closure
which is a staple of regular languages/regular expressions.

> function testing {
> echo test$1
> }
> export -f testing
> (echo x; echo y;) | parallel testing '{}'

Is this the one that "works"?  What is its goal?

If the goal is "I want to execute a shell function under the control of
an external command like find(1) or parallel(1)" then you have several
issues to overcome.  The first, which you seem to have found already, is
that the external command does not have any knowledge of the functions
defined by its parent shell process.  However, since you "exported" the
function (a bash-only concept by the way), any child bash processes which
inherited that function through the environment will be able to use it.

Thus, in your example, you have bash -> parallel -> bash.  (Presumably --
maybe parallel uses $SHELL instead of /bin/sh?  I literally do not know.)
Since the grandchild bash process inherits the "exported function" from
its ancestor, it is able to use it.

If parallel had spawned a /bin/sh instead of a bash, then the grandchild
would not have been able to use the function, since the inheritance of
functions through the environment is a feature of bash only.

I still don't have a clue what you are trying to DO, or what you are
actually ASKING.

Perhaps what you want is this:

while read foo; do
  testing "$foo"
done <<'EOF'

That's my best guess at the moment.  It would be nice if I did not have to

reply via email to

[Prev in Thread] Current Thread [Next in Thread]