[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: about the local not-on-every-function-separately var issue
From: |
Chet Ramey |
Subject: |
Re: about the local not-on-every-function-separately var issue |
Date: |
Mon, 29 Mar 2021 10:34:01 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 |
On 3/22/21 9:38 PM, Dale R. Worley wrote:
In my experience, a common reason is that the documentation does not
concentrate in one place that users are certain to read, a complete,
clear description of the situation. For instance, you give a complete,
clear description:
Bash uses "dynamic scope" when it expands variables. This means that
it looks first in the current functions local variables; if the variable
isn't found there, it looks in the caller's local variables, and then
in the caller's caller's local variables, and so on, until it reaches
the global scope.
Well, there is this in the FUNCTIONS section of the man page:
"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."
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/