Neelands
Neelands

Reputation: 13

Determining where a bash function was defined

I'm on a proprietary server; I know nothing about it. There's a bash function we use routinely to describe the server but if I write a bash script that uses that function it won't work.

So I want to load the original source file in the script. Is there a way to determine the location of the source file besides a "brute-force" search approach? If I do type -a thefunction I can see the definition but not where it was sourced from.

Upvotes: 1

Views: 130

Answers (2)

pwan
pwan

Reputation: 2914

Check out the 'Making xtrace more useful' section of http://wiki.bash-hackers.org/scripting/debuggingtips/

I tried adding the following near the top one of my scripts, and it looks like it gives the filename/line numbers for function calls.

set -x
export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

I tested on a CentOS 6.2 host with bash 4.1.2

Upvotes: 0

jordanm
jordanm

Reputation: 34964

This information isn't readily available. What may help is to start an interactive login shell with debugging (set -x) enabled. This will show you the source process in detail.

bash -ilx

When I initiate a bash shell like this, I see something like the following:

+++ . /etc/bash_completion.d/dd
++++ have dd
++++ unset -v have
++++ PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/sbin:/usr/sbin:/usr/local/sbin

This shows me that when . /etc/bash_completion/dd was sourced, PATH was modified (or possibly re-set to the same values).

Upvotes: 1

Related Questions