bug-bash
[Top][All Lists]
Advanced

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

Re: static vs. dynamic scoping


From: Chris F.A. Johnson
Subject: Re: static vs. dynamic scoping
Date: Tue, 9 Nov 2010 22:07:31 -0500 (EST)
User-agent: Alpine 2.00 (LMD 1167 2008-08-23)

On Tue, 9 Nov 2010, Eric Blake wrote:

On the Austin Group mailing list, David Korn (of ksh93 fame)
complained[1] that bash's 'local' uses dynamic scoping, but that ksh's
'typeset' uses static scoping, and argued that static scoping is saner
since it matches the behavior of declarative languages like C and Java
(dynamic scoping mainly matters in functional languages like lisp):

[1]
https://www.opengroup.org/sophocles/show_mail.tpl?CALLER=show_archive.tpl&source=L&listname=austin-group-l&id=14951

I'm trying to standardize the notion of local variables for the next
revision of POSIX, but before I can do so, I need some feedback on two
general aspects:
...
2. User aspect:
 Is anyone aware of a script that intentionally uses the full power of
dynamic scoping available through 'local' which would break if scoping
switched to static?  In particular, I know that the bash-completion
project has fought with local variable scoping issues; would it help or
hurt to switch to static scoping?

Here's a sample shell script that illustrates the difference between the
two scoping methods.

$ ksh -c 'function f1 { typeset a=local; f2; echo $a; };
 function f2 { echo $a; a=changed; };
 a=global; f1; echo $a'
global
local
changed

$ bash --posix -c 'function f1 { typeset a=local; f2; echo $a; };
 function f2 { echo $a; a=changed; };
 a=global; f1; echo $a'
local
changed
global
...

   I find the bash behaviour more logical, and I do use it in scripts.

--
   Chris F.A. Johnson, <http://cfajohnson.com>
   Author:
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)



reply via email to

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