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


Groups > gnu.bash.bug > #15258

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

Path csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail
From Greg Wooledge <wooledg@eeg.ccf.org>
Newsgroups gnu.bash.bug
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
Lines 53
Approved bug-bash@gnu.org
Message-ID <mailman.126.1564407803.1985.bug-bash@gnu.org> (permalink)
References <5D3E9154.2030007@tlinx.org> <20190729134312.GA1218@eeg.ccf.org>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
X-Trace usenet.stanford.edu 1564407804 13742 209.51.188.17 (29 Jul 2019 13:43:24 GMT)
X-Complaints-To action@cs.stanford.edu
To bug-bash@gnu.org
Envelope-to bug-bash@gnu.org
Mail-Followup-To bug-bash@gnu.org
Content-Disposition inline
In-Reply-To <5D3E9154.2030007@tlinx.org>
User-Agent Mutt/1.10.1 (2018-07-13)
X-detected-operating-system by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]
X-Received-From 139.137.100.1
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.23
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <https://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
X-Mailman-Original-Message-ID <20190729134312.GA1218@eeg.ccf.org>
X-Mailman-Original-References <5D3E9154.2030007@tlinx.org>
Xref csiph.com gnu.bash.bug:15258

Show key headers only | View raw


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.

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web