Differences between revisions 1 and 2
Revision 1 as of 2007-05-09 18:36:26
Size: 727
Editor: GreyCat
Comment: new question
Revision 2 as of 2007-05-09 18:40:42
Size: 775
Editor: GreyCat
Comment:
Deletions are marked like this. Additions are marked like this.
Line 18: Line 18:
I don't know. I think it has something to do with object oriented programming. But people keep asking it, so.... I don't know. I think it has something to do with [http://en.wikipedia.org/wiki/Reflection_%28computer_science%29 reflection]. But people keep asking it, so....

Anchor(faq83)

How do I determine whether a function is already defined?

There are several answers, all of which require Bash (or at least, non-Bourne) commands:

# These two are best:
if [[ $(declare -f foo) ]]     # it prints nothing, if undefined
if declare -f foo >/dev/null   # it also sets the exit status

# These are a little more obvious, but...
if [[ $(type foo 2>&1) = *\ is\ a\ function* ]]
if type foo >/dev/null 2>&1 && ! type -f foo >/dev/null 2>&1

A related question is, Why on earth does anyone want this? Why not just define the function already?

I don't know. I think it has something to do with [http://en.wikipedia.org/wiki/Reflection_%28computer_science%29 reflection]. But people keep asking it, so....

BashFAQ/083 (last edited 2022-11-26 06:06:57 by emanuele6)