Summary
Shell script functions allow you to place script code that's repeated throughout the script in a single place. Instead of having to rewrite blocks of code, you can create a function containing the code block and then just reference the function name in your script. The bash shell jumps to the function code block whenever it sees the function name used in the script.
You can even create script functions that return values. This allows you to create functions that interact with the script, returning both numeric and character data. Script functions can return numeric data by using the exit status of the last command in the function or using the return
command. The return
command allows you to programmatically set the exit status of your function to a specific value based on the results of the function.
Functions can also return values using the standard echo
statement. You can capture the output data using the backtick character as you would any other shell command. This enables...