1

If I have a function like so:

    #!/usr/bin/env bash

    function foo {

    }

and the above function is in .bash_profile, or somewhere.

If I use which foo it won't give me the location of this command in the shell script.

Is there some way for me to discover where it's located or is grep my best bet?

2 Answers2

4

This question was already answered here. So there are two things that can be derived from this fact:

  1. You should do a better research before posting your question here (but well, we all are lazy from time to time)
  2. There is a way better trick then grep to do what you want

So you can get a source file and a line number of a shell function in your PATH like this: (credit: this answer)

# Turn on extended shell debugging
shopt -s extdebug

# Dump the function's name, line number and fully qualified source file  
declare -F foo

# Turn off extended shell debugging
shopt -u extdebug
ddnomad
  • 1,978
  • 2
  • 16
  • 31
  • 1
    Or just use a subshell: (shopt -s extdebug; declare -F foo) to avoid having to restore the option state (and assume it was off beforehand). – Stéphane Chazelas Nov 27 '17 at 10:00
  • @StéphaneChazelas thanks for the tip! BTW is there a way to make it a script? – ddnomad Nov 27 '17 at 10:03
  • No, unless you source that script, as declare would have to be run from the shell invocation where that function is defined. You could make it a... function find_func() (shopt -s extdebug; declare -F -- "$@") – Stéphane Chazelas Nov 27 '17 at 10:10
  • Note that for functions imported from the environment, it would show as foo 0 environment so you would not get back to where the function was defined in the first place. – Stéphane Chazelas Nov 27 '17 at 10:12
1

which only searches the path. To see what bash really executes use the built-in command type:

type foo
xenoid
  • 8,888