[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: local functions
From: |
Eli Schwartz |
Subject: |
Re: local functions |
Date: |
Thu, 10 Dec 2020 09:04:39 -0500 |
On 12/10/20 8:34 AM, Christof Warlich wrote:
Hi,
$ help declare
says:
...
When used in a function, `declare' makes NAMEs local, as with the
`local'
command.
...
Furthermore, bash allows to define a function within another function:
$ f() {
echo f
declare -f g
g() {
echo g
}
}
$ f
f
g
But g is not local:
declare -f g; echo $?
You tried to *print* the definition, it failed, then later you defined
the function and did not use declare to do so.
The same applies for variables.
$ f() {
unset v
declare -p v
v='hello there'
}
$ f
bash: declare: v: not found
$ declare -p v
declare -- v="hello there"
declare -p is the variable-relevant "print definition" option. It does
not modify the variable type.
v is not local, due to being created non-local.
$ g
g
Thus, either the documentation or bash's behavior seens to be wrong, right?
Unless you see part of the documentation for declare which describes how
to create a function using declare, rather than creating the function using:
funcname() { ...; }
then the documentation for declare is correctly pointing out that
"declare" will "Declare variables and give them attributes" by
defaulting to local variables inside of functions.
The only confusion here is you seem to think that the special options
-f, -F, -p are obeying the description "Declare variables and give them
attributes" in the first place.
--
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User
OpenPGP_signature
Description: OpenPGP digital signature