bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "source" cmd creates entry in BASH_SOURCE etc. only if within functi


From: Chet Ramey
Subject: Re: "source" cmd creates entry in BASH_SOURCE etc. only if within function
Date: Thu, 04 Sep 2014 10:59:19 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.6.0

On 9/2/14, 2:59 PM, jim.avera@gmail.com wrote:

> Bash Version: 4.3
> Patch Level: 0
> Release Status: release
> 
> Description:
> 
> The "source" (or ".") command, if invoked from within a function,
> creates a call frame, setting $FUNCNAME[0] to "source" and
> $BASH_SOURCE[1] and $BASH_LINENO[1] to the location of the "source"
> statement.
> 
> However this does not occur if the "source" statement occurs in
> mainline code, i.e. outside of any function.

This is true.  The FUNCNAME variable is not visible outside functions, so
the assignment of `source' to FUNCNAME, which was done primarily for the
bash debugger's convenience, doesn't have any effect when outside a
function.

> 
> This means that code in "sourced" files can not reliably 
> display the location of their call, i.e., the location of
> the "source" (or ".") statement which invoked them.

But this is not.  If you want to see where the sourced file was invoked,
use ${BASH_SOURCE[1]}:${BASH_LINENO} or the `caller' builtin.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]