Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #15258 > unrolled thread

Re: Have var+func sourced in a subroutine but they don't seem to end up in same scope

Started byGreg Wooledge <wooledg@eeg.ccf.org>
First post2019-07-29 09:43 -0400
Last post2019-07-29 09:43 -0400
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Have var+func sourced in a subroutine but they don't seem to end up in same scope Greg Wooledge <wooledg@eeg.ccf.org> - 2019-07-29 09:43 -0400

#15258 — Re: Have var+func sourced in a subroutine but they don't seem to end up in same scope

FromGreg Wooledge <wooledg@eeg.ccf.org>
Date2019-07-29 09:43 -0400
SubjectRe: Have var+func sourced in a subroutine but they don't seem to end up in same scope
Message-ID<mailman.126.1564407803.1985.bug-bash@gnu.org>
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.

[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web