help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Closure concept in bash


From: Chet Ramey
Subject: Re: [Help-bash] Closure concept in bash
Date: Tue, 31 Jan 2012 17:10:28 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:8.0) Gecko/20111105 Thunderbird/8.0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 1/31/12 4:52 PM, Dan Douglas wrote:

> Here's a Korn shell "closure" of sorts. Notice illustrating the difference 
> from 
> Bash requires two different kinds of function definitions:
> 
> $ ( f() { typeset x=5; function g { echo "$x"; }; }; f; g; )
> 5
> 
> And this alone still doesn't allow you to do very much which you can't 
> already 
> do in Bash due to the way scoping works - using the context at the time the 
> function is called.

It works the same in bash if you remove the typeset.  The only reason it
works this way in ksh is that typeset doesn't create local variables in
`posix'-style functions; leaving it in simply disguises the creation of a
global variable.  (Add `echo $x' after the call to `g' to see.)

Chet
- -- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk8oZtQACgkQu1hp8GTqdKs1CgCeKERn8OKw/k8DoASjUghDxgVL
kP8AniDqBbo9Qp+ZgLsU+YryWJPguO2N
=mRoE
-----END PGP SIGNATURE-----



reply via email to

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