Differences between revisions 3 and 26 (spanning 23 versions)
Revision 3 as of 2007-07-31 19:42:19
Size: 1796
Editor: GreyCat
Comment: expand to cover "Determine whether a variable is defined"
Revision 26 as of 2016-11-23 18:40:14
Size: 2211
Editor: GreyCat
Comment: Reset button. Start over. My god, what was all that noise? Now wait and see how long until the page is re-vandalized by the well-meaning.
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq83)]] <<Anchor(faq83)>>
Line 3: Line 3:

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:
There are several ways to test these things, depending on the exact requirements. Most of the time, the desired test is ''whether a variable has a non-empty value''. In this case, we may simply use:
Line 7: Line 6:
if test -n "$var"
if [ -n "$var" ]
if test "$var"
if [ "$var" ]
if [[ -n $var ]]
if [[ $var ]]
# POSIX
if test "$var"; then
  echo "The variable has a non-empty value."
fi
Line 15: Line 12:
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 [#faq73 parameter expansion] tricks that can be used. Here is the simplest: If this fails for you because you use `set -u`, please see [[BashFAQ/112|FAQ 112]].

If we wish to distinguish between an ''empty'' variable and an ''unset'' variable, then we may use the `+` [[BashFAQ/073|parameter expansion]]:
Line 18: Line 17:
if [[ ${foo+defined} ]]
# This expansion results in nothing if foo is undefined. Therefore [[ returns false.
# If foo is defined (to either "" or something longer), the expansion returns "defined",
# and therefore [[ returns true.
# You could use any non-empty string in place of "defined", but readability is nice.
# POSIX
if test "${var+defined}"; then
  echo "The variable is defined."
fi
Line 25: Line 23:
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: The magic here is the `+`, ''not'' the word `defined`. We can use any non-empty word after the `+` sign. I prefer `defined` because it indicates what kind of test is being performed.

=== Setting a default value ===

If what we really want is to set a variable to a default value ''unless it already has a value'', then we may skip the test, and use the `=` parameter expansion:
Line 28: Line 30:
# 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
# POSIX
: "${var=default}"
Line 37: Line 34:
A related question is, ''Why on earth does anyone ''want'' this? Why not just define the function already?'' See [[BashFAQ/073|FAQ 73]] for details.
Line 39: Line 36:
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.... === Testing whether a function has been defined ===

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. Testing that a function is defined should rarely be necessary. Just define the function as you want it to be defined instead of worrying about what might or might not have been inherited from who-knows-where.

{{{
declare -F f >/dev/null # Bash only - declare outputs "f" and returns 0 if defined, returns non-zero otherwise.
typeset -f f >/dev/null # Bash/Ksh - typeset outputs the entire function and returns 0 if defined, returns non-zero otherwise.
[[ $(type -t f) = function ]] # Bash-only - "type" outputs "function" if defined. In ksh (and mksh), the "type" alias for "whence -v" differs.

# Bash/Ksh. Workaround for the above, but the first two are preferable.
isFunction() [[ $(type ${BASH_VERSION:+-t} "$1") == ${KSH_VERSION:+"$1 is a "}function ]]; isFunction f
}}}

How do I determine whether a variable is already defined? Or a function?

There are several ways to test these things, depending on the exact requirements. Most of the time, the desired test is whether a variable has a non-empty value. In this case, we may simply use:

# POSIX
if test "$var"; then
  echo "The variable has a non-empty value."
fi

If this fails for you because you use set -u, please see FAQ 112.

If we wish to distinguish between an empty variable and an unset variable, then we may use the + parameter expansion:

# POSIX
if test "${var+defined}"; then
  echo "The variable is defined."
fi

The magic here is the +, not the word defined. We can use any non-empty word after the + sign. I prefer defined because it indicates what kind of test is being performed.

Setting a default value

If what we really want is to set a variable to a default value unless it already has a value, then we may skip the test, and use the = parameter expansion:

# POSIX
: "${var=default}"

See FAQ 73 for details.

Testing whether a function has been defined

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. Testing that a function is defined should rarely be necessary. Just define the function as you want it to be defined instead of worrying about what might or might not have been inherited from who-knows-where.

declare -F f >/dev/null       # Bash only - declare outputs "f" and returns 0 if defined, returns non-zero otherwise.
typeset -f f >/dev/null       # Bash/Ksh - typeset outputs the entire function and returns 0 if defined, returns non-zero otherwise.
[[ $(type -t f) = function ]] # Bash-only - "type" outputs "function" if defined. In ksh (and mksh), the "type" alias for "whence -v" differs.

# Bash/Ksh. Workaround for the above, but the first two are preferable.
isFunction() [[ $(type ${BASH_VERSION:+-t} "$1") == ${KSH_VERSION:+"$1 is a "}function ]]; isFunction f

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