commands:builtin:local

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
commands:builtin:local [2012/01/31 14:41]
ormaaj quick draft. Typed in browser and gotta reboot.
commands:builtin:local [2012/02/01 09:02]
ormaaj Turns out a lot of this is wrong. Sorry. :(
Line 10: Line 10:
 </​WRAP>​ </​WRAP>​
  
 +Warning - mountains of fail below on my part. Even I don't understand this after all. Going back to do more testing.
 +<WRAP hide>
 The most important thing to know about variable scope in Bash is that the variables visible from within the body of a function are always the same as those which are visible //at the time a function is called//. Two equivalent ways of stating how this works: The most important thing to know about variable scope in Bash is that the variables visible from within the body of a function are always the same as those which are visible //at the time a function is called//. Two equivalent ways of stating how this works:
  
Line 18: Line 20:
 This differs from the majority of other languages except those with so-called [[http://​community.schemewiki.org/?​dynamic-scope|dynamic scope]], though shell functions are so vastly unconventional in other ways that it's difficult to judge whether this behavior is good or bad by contrast to the conventions of other languages. If a function doesn'​t take care to locally define the variables used by commands then the value of any given variable can differ depending on the context from which it is called. Alternatively,​ almost all gotchas regarding use of global variables in most languages also apply at least equally to Bash, if not moreso, as Bash lacks namespaces and a powerful type system. ​ This differs from the majority of other languages except those with so-called [[http://​community.schemewiki.org/?​dynamic-scope|dynamic scope]], though shell functions are so vastly unconventional in other ways that it's difficult to judge whether this behavior is good or bad by contrast to the conventions of other languages. If a function doesn'​t take care to locally define the variables used by commands then the value of any given variable can differ depending on the context from which it is called. Alternatively,​ almost all gotchas regarding use of global variables in most languages also apply at least equally to Bash, if not moreso, as Bash lacks namespaces and a powerful type system. ​
  
-This discussion inevitably brings up questions about closures, first-class-functions,​ higher-order-functions,​ continuations,​ function-objects,​ and the like. The answer is that these concepts are rarely applicable to Bash for the aforementioned reason that "​functions"​ are just distantly reminiscent of what one would typically call a "​conventional"​ function. Function definitions can't be nested [[howto/​collapsing_functions|in the usual sense]]+This discussion inevitably brings up questions about closures, first-class-functions,​ higher-order-functions,​ continuations,​ function-objects,​ and the like. The answer is that these concepts are rarely applicable to Bash for the aforementioned reason that "​functions"​ are just distantly reminiscent of what one would typically call a "​conventional"​ function. Function definitions can't be nested [[howto/​collapsing_functions|in the usual sense]], nor can they be passed around to, or returned from functions either as objects or by reference.
  
 ===== Examples ===== ===== Examples =====
Line 51: Line 53:
  
 ===== Portability considerations ===== ===== Portability considerations =====
- 
-  * In Bash, Zsh, and Mksh, locals referenced as arithmetic variables become local at the start of the [[typeset]] command (and are therefore always zero), while parameter expansions have the value of the next-outermost scope. The only exception is the original Ksh. 
- 
-<​code>​ 
-# ksh93 only. Infinite recursion in others. 
-$ f() { typeset -i n=${1:​---n};​ if ((n)); then printf $n; f; fi }; f 5 
-54321 
- 
-# Bash et al. 
-$ f() { typeset -i n=${1:​-$n-1};​ if ((n)); then printf -- $n; f; fi }; f 5 
-54321 
-</​code>​ 
  
   * ''​local''​ is not specified by POSIX. Additionally,​ ''​local''​ is Bash-only. It is identical to [[declare]] in every way, and takes all the same options, except that usage outside of a function is an error. Using ''​declare''​ within a function has the same effect as local unless the -g option is given. ''​typeset''​ is technically more portable than both ''​local''​ and ''​declare'',​ but labeled as "​depreciated"​ in the Bash manpage.   * ''​local''​ is not specified by POSIX. Additionally,​ ''​local''​ is Bash-only. It is identical to [[declare]] in every way, and takes all the same options, except that usage outside of a function is an error. Using ''​declare''​ within a function has the same effect as local unless the -g option is given. ''​typeset''​ is technically more portable than both ''​local''​ and ''​declare'',​ but labeled as "​depreciated"​ in the Bash manpage.
  
   * POSIX does not specify function local variables and their behavior is therefore not formally defined and often largely undocumented. The Korn shell and derivatives including Bash and Zsh thankfully mostly agree on semantics (when using the "​POSIX-style"​ function definition syntax), and overload the typeset builtin (or declare and local) to declare variables as local.   * POSIX does not specify function local variables and their behavior is therefore not formally defined and often largely undocumented. The Korn shell and derivatives including Bash and Zsh thankfully mostly agree on semantics (when using the "​POSIX-style"​ function definition syntax), and overload the typeset builtin (or declare and local) to declare variables as local.
 +</​WRAP>​
 ===== See also ===== ===== See also =====
  
-  * http://​wiki.bash-hackers.org/​scripting/​basics#​variable_scope ​(todo: do something about this)+  * http://​wiki.bash-hackers.org/​scripting/​basics#​variable_scope
  • commands/builtin/local.txt
  • Last modified: 2012/04/23 01:30
  • by ormaaj