[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: "unset var" pops var off variable stack instead of unsetting it
From: |
Chet Ramey |
Subject: |
Re: "unset var" pops var off variable stack instead of unsetting it |
Date: |
Wed, 22 Mar 2017 15:40:39 -0400 |
> The effect of unset on a local was what I had in mind, but really the
> manual says very little about scope. All it says right now is:
>
> "Variables local to the function may be declared with the local builtin
> command. Ordinarily, variables and their values are shared between the
> function and its caller."
>
> Which doesn't exactly describe dynamic scope even for those that know
> what that means.
Here's what I have to start:
Variables local to the function may be declared with the local builtin
command. Ordinarily, variables and their values are shared between the
function and its caller. If a variable is declared local, the vari-
able's visible scope is restricted to that function and its children
(including the functions it calls). Local variables "shadow" variables
with the same name declared at previous scopes. For instance, a local
variable declared in a function hides a global variable of the same
name: references and assignments refer to the local variable, leaving
the global variable unmodified. When the function returns, the global
variable is once again visible.
The shell uses dynamic scoping to control a variable's visibility
within functions. With dynamic scoping, visible variables and their
values are a result of the sequence of function calls that caused exe-
cution to reach the current function. The value of a variable that a
function sees depends on its value within its caller, if any, whether
that caller is the "global" scope or another shell function. This is
also the value that a local variable declaration "shadows", and the
value that is restored when the function returns.
For example, if a variable var is declared as local in function func1,
and func1 calls another function func2, references to var made from
within func2 will resolve to the local variable var from func1, shadow-
ing any global variable named var.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://cnswww.cns.cwru.edu/~chet/
- Re: "unset var" pops var off variable stack instead of unsetting it, (continued)
- Re: "unset var" pops var off variable stack instead of unsetting it, Chet Ramey, 2017/03/18
- Re: "unset var" pops var off variable stack instead of unsetting it, Eric Blake, 2017/03/20
- Re: "unset var" pops var off variable stack instead of unsetting it, Chet Ramey, 2017/03/20
- Re: "unset var" pops var off variable stack instead of unsetting it, Stephane Chazelas, 2017/03/20
- Re: "unset var" pops var off variable stack instead of unsetting it, Chet Ramey, 2017/03/20
- Re: "unset var" pops var off variable stack instead of unsetting it, Stephane Chazelas, 2017/03/21
- Re: "unset var" pops var off variable stack instead of unsetting it, Martijn Dekker, 2017/03/22
Re: "unset var" pops var off variable stack instead of unsetting it, Chet Ramey, 2017/03/18
Re: "unset var" pops var off variable stack instead of unsetting it, Chet Ramey, 2017/03/18
Re: "unset var" pops var off variable stack instead of unsetting it, Peter & Kelly Passchier, 2017/03/19
Re: "unset var" pops var off variable stack instead of unsetting it, 渡邊裕貴, 2017/03/20
Re: "unset var" pops var off variable stack instead of unsetting it, Stephane Chazelas, 2017/03/20
Re: "unset var" pops var off variable stack instead of unsetting it, Greg Wooledge, 2017/03/20