bash: Conditionally declaring functions by nesting them
A function
definition in bash assigns the compound statement body to the given name. That can happen inside another function, meaning that the nested name will only be bound when the outer function is evaluated. There is no scope implied in this, just timing.
For example:
function outer1() {
function inner() { echo "outer1 > inner" ; }
}
function outer2() {
function inner() { echo "outer2 > inner" ; }
}
# Initially, inner not defined
inner # ERROR
# Running outer1 defines inner
outer1
inner
# outer1 > inner
# Running outer1 redefines inner
outer2
inner
# outer2 > inner
Published on: 27 Sep 2022