Functions in a bash script question...
by taylorkh from LinuxQuestions.org on (#4WFQT)
Based upon some excellent advice from members of this forum I am redesigning a script which has gotten a little to complicated. Most of the code will be broken into functions and only the main flow control logic will be in the "main" part of the script. It is already looking much more manageable. However...
I have been playing with some simple scripts and functions to get my head wrapped around how bash handles functions. From what I am seeing I must define my function FIRST and then call the function. I will have a situation where function3 may need to call function5. Does this mean I must define function3 before funtion3 and all functions before I code the control loops and if statements? That seems contrary to "top down" programming. Sort of inside out programming :)
In other languages and environments I could place functions, especially utility type functions, in a separate file and then "assimilate" that file into the program at run time in an interpreted environment or at compile/pseudo-compile time. I suspect there is a way to handle this in bash. If I source the file containing the functions from within the main script, will all the functions be available in the main script?
TIA,
Ken


I have been playing with some simple scripts and functions to get my head wrapped around how bash handles functions. From what I am seeing I must define my function FIRST and then call the function. I will have a situation where function3 may need to call function5. Does this mean I must define function3 before funtion3 and all functions before I code the control loops and if statements? That seems contrary to "top down" programming. Sort of inside out programming :)
In other languages and environments I could place functions, especially utility type functions, in a separate file and then "assimilate" that file into the program at run time in an interpreted environment or at compile/pseudo-compile time. I suspect there is a way to handle this in bash. If I source the file containing the functions from within the main script, will all the functions be available in the main script?
TIA,
Ken