I fully agree with grail, especially since both Bash and POSIX shells allow you to define a function practically anywhere. You can do things like
Code:
setfoo () {
if [ "$1" = "y" ]; then
foo () {
echo "foo: yes"
}
else
foo () {
echo "foo: no"
}
fi
}
so that for example
Code:
setfoo y
foo
setfoo n
foo
will output
If you want a niftier example, define function setfoo like this:
Code:
setfoo () {
eval "foo () {
echo \"$*:\" \"\$@\"
}"
}
The eval there is needed since we want $* from the outer function. To make sure "$@" is not evaluated within the eval, we escape it to "\$@".
Then, running
Code:
setfoo a b c
foo 1 2 3
foo 3 2 1
setfoo d e f
foo 4 5 6
foo 6 5 4
will output
Code:
a b c: 1 2 3
a b c: 3 2 1
d e f: 4 5 6
d e f: 6 5 4