[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Q: interoperability of local vars and option nounset
From: |
Chet Ramey |
Subject: |
Re: Q: interoperability of local vars and option nounset |
Date: |
Mon, 28 Feb 2011 22:48:37 -0500 |
User-agent: |
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13) Gecko/20101207 Lightning/1.0b2 Thunderbird/3.1.7 |
On 2/23/11 8:36 AM, Nardmann, Heiko wrote:
> Hi together,
>
> short: is there any way to get informed about unset local variable while
> having option 'nounset' activated?
>
> Long: the problem with 'local' is that variable declared this way are
> visible inside the children, too. So the behaviour of option 'nounset'
> depends on the way a function is called - either with a caller function
> with the (locally) unset variable already set or without one.
Yes. This is a consequence of bash's dynamic scoping.
I'm not sure I understand your question. Your example script demonstrates
one of the properties of dynamic scoping: a function can find variables
up the call chain all the way to the global variable context.
If your question is whether or not the call to `g' can result in an unset
variable error even though f has a local copy already declared, the answer
is no. That is ksh93-style static scoping, and bash does not have it.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU chet@case.edu http://cnswww.cns.cwru.edu/~chet/