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?

1 Answer 1

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.

1
  • That's the fix! Thanks for helping me see what was staring me in the face :-)
    – jalanb
    Commented May 30, 2018 at 13:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .