[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Have var+func sourced in a subroutine but they don't seem to end up
From: |
Greg Wooledge |
Subject: |
Re: Have var+func sourced in a subroutine but they don't seem to end up in same scope |
Date: |
Mon, 29 Jul 2019 09:43:12 -0400 |
User-agent: |
Mutt/1.10.1 (2018-07-13) |
On Sun, Jul 28, 2019 at 11:25:24PM -0700, L A Walsh wrote:
> util_fn () {
>
> declare [-x] foo=1
> declare [-x] bar=${foo}2
>
> real_util_fn() {
> makes use of bar to get 'foo2'
> }
>
> real_util_fn "$@"
> }
You do realize that despite your indentation, and despite the definition
of real_util_fn being executed inside the body of a second function, both
of these functions are equal. Right?
There's only one function namespace in bash. All functions are global.
There's no such thing as a function that only exists while executing a
different function, unless of course you bring subshells into the picture.
Which so far you haven't.
> Now 'real_util_fn' behaves like the data, i.e. if I export them
>
> (export bar & export -f util_fn real_util_fn)
>
> only 'util_fn' is accessible in the file doing the include --
> i.e. 'real_util_fn' appears to be local to util_fn.
Nonsense. Functions are not "local" to other functions. All functions
are global.
> Without the extra function for encapsulating the data+func
> it appears that real_util_fn ends up in the scope of
> "include's" parent while the variables end up in 'include's
> scope because of the declare (the -x seems to be relatively ignored).
All functions are global. Always.
> Why does bash disallow this?:
>
> declare -f util_fn
> util_fn() { :;}
>
> or
>
> declare util_fn() { :; }
>
> which, it seems would declare util_fn local to a subs
There is no such thing as a function being "local" to anything.
All functions are global.