Using Functions
A function is a part of a script that performs a specific subtask and that can be called by name from other parts of the script. Placing parentheses after the function name and enclosing the lines that make up the function within curly braces is how functions are defined:
myfn() { commands }
The keyword function
may optionally precede the function name. In either event, the function is called by name as if it were an ordinary internal or external command.
Functions are useful in helping to create modular scripts. For instance, if your script needs to perform half a dozen distinct computations, you can place each computation in a function and then call them all in sequence. Listing 11.6 demonstrates the use of functions in a simple program that copies a file but aborts with an error message if the target file already exists. This script accepts a target and a destination filename and must pass those filenames to the functions.