Defining a Function
You can define a function in the following places:
- On the command-line
- In a shell script
- In a function library
- In a shell configuration file
Defining a function from the command-line can be useful if you need to do any quick testing or experimentation with new code. Here’s what it looks like:
[donnie@fedora ~]$ howdy() { echo "Howdy, world!" ; echo "How's it going?"; }
[donnie@fedora ~]$ howdy
Howdy, world!
How's it going?
[donnie@fedora ~]$
The name of this function is howdy
, and the ()
tells the shell that this is a function. The code for the function is within the pair of curly braces. The function consists of two echo
commands, which are separated by a semi-colon. Note that you need to place another semi-colon at the end of the final command. You also need to have one blank space between the first curly brace and the first command, and the final semi-colon and the closing curly...