6

I have 2 small functions to abbreviate set -x / set +x, namely:

sx () {
    export PS4='+ [${BASH_SOURCE##*/}:${LINENO}]'
    set -x
}

and

sz () {
    set +x
    export PS4=
}

These 2 functions are in a file functons.sh which is sourced from ~/.bashrc, and visible online here.

Immediately after logging in to a new bash session, I try to find out where the sz() function is like this:

$ shopt -s extdebug; declare -F sz; shopt -u extdebug
sz 25 /Users/jab/src/git/hub/jab/src/bash/keyboard/z.sh

This shows that bash thinks the sz() function is declared in z.sh, but at that line there is a different function, zzz(), online here.

The result for the sx() function (and all my myriad other functions) is correct

$ shopt -s extdebug; declare -F sx; shopt -u extdebug
sx 428 /Users/jab/src/git/hub/jab3/src/bash/functons.sh

Why does the declare -F command get the wrong result for the sz function?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
jalanb
  • 578
  • 4
  • 15

1 Answers1

10

You also have an alias in functons.shwith the same name as a function in your other file.

In functons.sh:

alias zzz=sz

In z.sh:

zzz () {
    df -h
}

This confuses bash.

Example:

$ cat f1
foo () { echo hello; }
alias xfoo=foo

$ cat f2
xfoo () { echo beep; }

$ source f1
$ source f2
$ shopt -s extdebug
$ declare -F foo
foo 1 f2

Without the xfoo alias in f1:

$ source f1
$ source f2
$ shopt -s extdebug
$ declare -F foo
foo 1 f1

The bash manual also includes the text

Aliases are confusing in some uses.

under the "BUGS" heading.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • That's [the fix](https://github.com/jalanb/jab/commit/c4fc8a45f3f192bcc9dd4949cb207fc3823ae4b8)! Thanks for helping me see what was staring me in the face :-) – jalanb May 30 '18 at 13:25