I personally believe that there are as many ways to write a shell script as there are people writing shell scripts.
Each individual, even if they follow similar thought processes, will apply themselves to the task with various levels of knowledge, efficiency, and dedication.
One thing I think that all coders have in common, is the tendency to re-use their code — especially after they learn to write it well, and are not constantly improving it.
There is one means of re-using code that is essential to learn, as it can enable a single block of code to be called up for use multiple times within a script.
Functions — specifically user-defined functions — are the means by which blocks of code can be re-used within a script.
Essentially described as scripts within scripts, functions can be created once, customized as needed, and called throughout the script — an unlimited number of times — to perform.
Take note, in case it is not already obvious, that functions must be defined before they are called, and that the commands/scripts in functions will not be run/interpreted until they are called, or executed.
The Syntax of a Function
Functions are defined with a unique name that is used to identify (call) them later on.
Function names should follow the same rules as variable names; functions are essentially glorified variables themselves.
Curly braces are used to indicate the beginning and end of a function by enclosing its code.
A function can be defined in the following manner:
function FunctionName { actions(s) to perform }
Optionally, a function can defined in the following manner (without the word “function”, but with unused parenthesis after the name):
FunctionName () { actions(s) to perform }
Either way that a function is defined, it is called/referenced in the same manner: by typing its name.
Function Variables
Variables inside functions can — and sometimes should — be localized.
By this, I mean that normal variables — global variables — that can be used anywhere in a shell script, may not be the ideal choice for use within functions.
Using local variables will allow them to be contained within a function, to prevent them from being used outside of the function and causing confusion.
To define local variables, simply add “local ” before a variable is defined.
function FunctionName { local FunctionVariable=value } FunctionName
Passing Arguments
If you’re a programmer, you may already be familiar with the fact that arguments, or values, can be passed to a function to be used within the function.
The syntax for passing those arguments in shell scripts is a bit different than many other languages.
Arguments can be specified, one after another, separated by spaces, after the function name when it is called.
Those arguments are then referenced within the function as variables $1, $2, $3, etc. (in chronological order).
Let’s take a look at an example that puts together a number of things we have learned from recent lessons, using a function with arguments.
function TransportationFunction { for var in "$@" do echo Some people travel via $var done } TransportationFunction horseback bicycle car boat train plane TransportationFunction land sea air
Returning Values
In most programming languages, return is used to send values back to the original calling script.
In shell scripts, return is used to indicate the return status at the end of a function’s execution.
Return values are similar to exit codes. A return value of 0 normally indicates successful execution, while other values may indicate problems.
You can use return to return a number, such as the value of a calculation, but it is not recommended.
The return value is assigned to the $? variable.
A Functional Example
In this example, you will see how easy it is to create a function that can be used throughout the remainder of the script, in this case to shorten the “ls -al” command to a single character.
#!/bin/bash function l { ls -al } l exit 0
Conclusion
These details just barely scratch the surface of what can be done with functions, and how they can be used.
Test out some possibilities as they come to mind, and see what you come up with!