Most (all?) shells have the means of creating new "internal" commands. This
is done by creating shell
functions. Shell functions are just like those
in a programming language. Sets of commands are grouped together and jointly
called by a single name.
The format for functions is:
Functions can be defined anywhere, including from the command line.
need to do is simply type in the lines one at a time, similar to the way shown
above. The thing to bear in mind is that if you type a function from a command
line, once you exit that shell, the function is gone.
Shell functions have the ability to accept arguments, just like commands. A
simple example is a script that looks like this:
The output would be
Here we need to be careful. The variable
$1 is the positional parameter from
the call to the display function and not to the script. We can see this when we
change the script to look like this:
Lets call the script display.sh and start it like this:
The output would then look like this:
The first echo shows us the parameter from the command line
and the second
one shows us the parameter from the function.