In this is the scenario, need to call func1
from Main_Func
. How do I call it?
Main_Func() { <code> } Initialize_func() { func1() { <code> } }
For func1
to be defined, you will first have to have called Initialize_func
at least once. Then you may call func1
as just func1
.
Example:
outer1 () { echo 'in outer1' inner } outer2 () { echo 'in outer2' inner () { echo 'in inner' } } # First example explained below: outer1 # Second example explained below: outer2 outer1
Calling outer1
without calling outer2
in this example will not work since inner
is not yet defined:
$ ksh93 script.sh in outer1 script.sh[3]: inner: not found [No such file or directory]
Calling outer2
first and then outer1
works:
$ ksh93 script.sh in outer2 in outer1 in inner
ksh
will put your func1
function in the same "scope" as the other functions. It's not as in C++ or other object oriented languages that func1
somehow becomes a sub-function or method in some inner scope of Initialize_func
.
This is regardless of whether you use the Bourne shell function syntax as above or define your functions using the function
keyword of the Korn shell.
foo() command
) as opposed to the Korn one (function foo { ...; }
), ksh93 doesn't do any local scope at all even for variables. Still, functions defined within functions using the Korn syntax are not local to the function either.CommentedJan 12, 2018 at 8:17