Differences between revisions 1 and 5 (spanning 4 versions)
Revision 1 as of 2007-05-09 18:36:26
Size: 727
Editor: GreyCat
Comment: new question
Revision 5 as of 2008-05-22 22:40:32
Size: 1825
Editor: GreyCat
Comment: clean up, adjust link
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
== How do I determine whether a function is already defined? == == How do I determine whether a variable is already defined?  Or a function? ==
Line 4: Line 4:
There are several answers, all of which require Bash (or at least, non-Bourne) commands: There are several ways to determine whether a variable is defined to have a non-empty value. Here are the most common ones, in order from most portable to least portable:
Line 7: Line 7:
if test -n "$var"
if [ -n "$var" ]
if test "$var"
if [ "$var" ]
if [[ -n $var ]]
if [[ $var ]]
}}}

If you need to distinguish between a variable that is ''undefined'' and one that is ''defined but empty'', then it becomes much trickier. There is no explicit shell command to test for existence of a variable, but there are some [:BashFAQ/073:parameter expansion] tricks that can be used. Here is the simplest:

{{{
# Bourne
if test "${foo+defined}"
}}}

This expansion results in nothing if foo is undefined. Therefore test returns false.
If foo is defined (to either "" or something longer), the expansion returns "defined", and therefore test returns true.
You could use any non-empty string in place of "defined", but readability is always nice.

For determining whether a function with a given name is already defined, there are several answers, all of which require Bash (or at least, non-Bourne) commands:

{{{
# Bash
Line 18: Line 41:
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 variable is already defined? Or a function?

There are several ways to determine whether a variable is defined to have a non-empty value. Here are the most common ones, in order from most portable to least portable:

if test -n "$var"
if [ -n "$var" ]
if test "$var"
if [ "$var" ]
if [[ -n $var ]]
if [[ $var ]]

If you need to distinguish between a variable that is undefined and one that is defined but empty, then it becomes much trickier. There is no explicit shell command to test for existence of a variable, but there are some [:BashFAQ/073:parameter expansion] tricks that can be used. Here is the simplest:

# Bourne
if test "${foo+defined}"

This expansion results in nothing if foo is undefined. Therefore test returns false. If foo is defined (to either "" or something longer), the expansion returns "defined", and therefore test returns true. You could use any non-empty string in place of "defined", but readability is always nice.

For determining whether a function with a given name is already defined, there are several answers, all of which require Bash (or at least, non-Bourne) commands:

# Bash
# 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)