Lesmana Zimmer wrote:
> the bash manual says that BASH_SOURCE shows filename where a function is
> defined
> (and not the filename from where the function is called).
No, this is not true. Nowhere it says that BASH_SOURCE holds the
filename where FUNCNAME is _defined_. In fact, the documentation for
BASH_LINENO uses almost the exact same wording, but it holds the line
number where FUNCNAME is _called_. So, at least the manual is not
clear about it. Below I collected all relevant passages from the
manual and made some comments. Still further below, you can find a
suggestion how the manual should be changed to have a clear
definition.
Concerning the bug report, I agree that BASH_SOURCE is correct if the
manual is updated correspondingly. Earlier, somehow, I felt that
writing
FUNCNAME[i] was called from file BASH_SOURCE[i] at line BASH_LINENO[i]
in a shell script is more correct than what needs to be written
currently, namely
FUNCNAME[i] was called from file BASH_SOURCE[i+1] at line BASH_LINENO[i]
The following is from the bash info pages. My comments are not
indented.
`BASH_SOURCE'
An array variable whose members are the source filenames
corresponding to the elements in the `FUNCNAME' array variable.
This could mean the source filename where FUNCNAME was called OR the
source filename where FUNCNAME is defined. Currently it holds the
filename where FUNCNAME is _defined_.
`FUNCNAME'
An array variable containing the names of all shell functions
currently in the execution call stack. The element with index 0
is the name of any currently-executing shell function. The
bottom-most element is `"main"'. This variable exists only when a
shell function is executing. Assignments to `FUNCNAME' have no
effect and return an error status. If `FUNCNAME' is unset, it
loses its special properties, even if it is subsequently reset.
No problem with the above.
`BASH_LINENO'
An array variable whose members are the line numbers in source
files corresponding to each member of FUNCNAME.
This is unclear again. Essentially the same formulation as for
BASH_SOURCE is used. Again, it could refer to the line number where
FUNCNAME is defined or where FUNCNAME was called. Currently it holds
the line number where FUNCNAME is _called_.
`${BASH_LINENO[$i]}' is the line number in the source file where
`${FUNCNAME[$i]}' was called
Ok.
(or `${BASH_LINENO[$i-1]}' if
referenced within another shell function).
The above does not make sense. It should be removed. First of all
the index should be $i+1 and not $i-1. Second, if referenced within
another shell function also the index of FUNCNAME should be
incremented.
The corresponding
source file name is `${BASH_SOURCE[$i]}'.
This is unclear again. Source file where FUNCNAME was _called_ from or
where FUNCNAME is _defined_?
Use `LINENO' to obtain the current line number.
Ok.
So BASH_SOURCE and BASH_LINENO are not defined consistently and the
manual is very vague about them. In one case a filename where a
function is _defined_ is referenced and in the other case a line
number where a function is _called_.
Maybe the best solution would be to update the manual. This would be
my suggestion:
`BASH_SOURCE'
An array variable whose members are the source filenames where
the corresponding elements in the `FUNCNAME' array variable are
defined. For instance the function `${FUNCNAME[$i]}' is defined
in the file `${BASH_SOURCE[$i]}' (and was called from file
`${BASH_SOURCE[$i+1]}'. See `FUNCNAME'.
`BASH_LINENO'
An array variable whose members are the line numbers in source
files where the corresponding members of FUNCNAME where called.
For instance the function `${FUNCNAME[$i]}' was called at line
number `${BASH_LINENO[$i]}' (in the file `${BASH_SOURCE[$i+1]}').
Use `LINENO' to obtain the current line number. See `FUNCNAME'.
`FUNCNAME'
An array variable containing the names of all shell functions
currently in the execution call stack. The element with index 0
is the name of any currently-executing shell function. The
bottom-most element is `"main"'. This variable exists only when a
shell function is executing. Assignments to `FUNCNAME' have no
effect and return an error status. If `FUNCNAME' is unset, it
loses its special properties, even if it is subsequently reset.
This variable is most useful together with `BASH_SOURCE' and
`BASH_LINENO'. For instance `${FUNCNAME[0]}' is the name of the
currently running function, it was called in file
`${BASH_SOURCE[1]}' at line number `${BASH_LINENO[0]}'. To print
a back trace of the call stack to stderr use the following
function
BT () {
i=1 # leave out BT from the call stack; set to 0 to include BT
until test "${FUNCNAME[$i]}" = "main"
do
echo "${FUNCNAME[$i]} called from
${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]}" >&2
let i++
done
}
--
BASH_SOURCE is not correct
https://bugs.launchpad.net/bugs/318678
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
--
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs