Text of lists.gnu.org › archive › html › bug-bash › 2014-09 › msg00007.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout.

Open the live page

"source" cmd creates entry in BASH_SOURCE etc. only if within function

bug-bash [ Top ][ All Lists ] [ Date Prev ][ Date Next ][ Thread Prev ][ Thread Next ][ Date Index ][ Thread Index ] "source" cmd creates entry in BASH_SOURCE etc. only if within function From : jim . avera Subject : "source" cmd creates entry in BASH_SOURCE etc. only if within function Date : Tue, 2 Sep 2014 11:59:11 -0700 (PDT) Configuration Information [Automatically generated, do not change]: Machine: x86_64 OS: linux-gnu Compiler: gcc Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-unknown-linux-gnu' -DCONF_VENDOR='unknown' -DLOCALEDIR='/usr/local/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I. -I./include -I./lib -g -O2 uname output: Linux lxjima 3.11.0-26-generic #45-Ubuntu SMP Tue Jul 15 04:02:06 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux Machine Type: x86_64-unknown-linux-gnu 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 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. Repeat-By: cat <<'EOF' >/tmp/sourced echo "\$@=$@ \$0=$0 #FUNCNAME=${#FUNCNAME[@]}" for ((i=0 ; i < ${#FUNCNAME[@]}; i=i+1)) ; do echo "(i=$i) FUNCNAME ${FUNCNAME[$i]} called from line ${BASH_LINENO[$i]} in ${BASH_SOURCE[$i+1]}" done EOF cat <<'EOF' >/tmp/test #!/bin/bash myfunc() { echo "----- Sourcing from inside a function:" source /tmp/sourced } myfunc echo "----- Sourcing from mainline code:" source /tmp/sourced EOF bash /tmp/test Actual Results: ----- Sourcing from inside function: $@= $0=/tmp/test #FUNCNAME=3 (i=0) FUNCNAME source called from line 4 in /tmp/test (i=1) FUNCNAME myfunc called from line 6 in /tmp/test (i=2) FUNCNAME main called from line 0 in ----- Sourcing directly from mainline code: $@= $0=/tmp/test #FUNCNAME=0 Expected Results: ----- Sourcing from inside function: $@= $0=/tmp/test #FUNCNAME=3 (i=0) FUNCNAME source called from line 4 in /tmp/test (i=1) FUNCNAME myfunc called from line 6 in /tmp/test (i=2) FUNCNAME main called from line 0 in ----- Sourcing directly from mainline code: $@= $0=/tmp/test #FUNCNAME=1 (i=0) FUNCNAME source called from line 8 in /tmp/test [(i=1) FUNCNAME main called from line 0 in ] [Prev in Thread] Current Thread [ Next in Thread ] "source" cmd creates entry in BASH_SOURCE etc. only if within function , jim . avera <= Re: "source" cmd creates entry in BASH_SOURCE etc. only if within function , Chet Ramey , 2014/09/04 Prev by Date: Re: nameref bug?

Next by Date: Re: nameref bug? Previous by thread: Re: nameref bug? Next by thread: Re: "source" cmd creates entry in BASH_SOURCE etc. only if within function Index(es): Date Thread