Shell Functions  «Prev  Next»
Lesson 3Defining a function
ObjectiveCreate a correctly formed shell script function.

Where to define a function in Shell script?

Question: In which section of a shell script should a function be defined? A function in a shell script can be defined anywhere in the script file. However, it is common practice to define all functions at the beginning of the script, before they are called or referenced. This helps make the code more organized and easier to read, as all the functions are grouped together and can be easily located. In addition, if a function is defined after it is called or referenced in the script, the script will fail to execute because the function has not been defined yet. Therefore, it is important to ensure that functions are defined before they are called or referenced in the script.

Function Definition

A function should be defined at the top of your script, before they are called. In this lesson, we will look at creating functions. Calling functions will be addressed later. Defining a function puts its name and commands into memory on your system. The commands are not run when the function is defined. This function definition stays in memory until the shell it was created in dies or until you remove it. Look at the MouseOver below to learn the syntax of a function definition.

function_name (){

command 1

…

}
 
function_name, This is a name you have decided to assign to this function.
(), Follow the function name with opening and closing parentheses.
{, Precede the function commands with an opening curly brace. This is usually placed on a line by itself.
command 1, Your first command inside the function. Commands are usually indented by two spaces from the position of the curly brace.
…, Place any other commands here.
}

The function ends with a curly brace, which is usually on a line by itself.

A sample function

The sample function below contains three lines of code that pause the screen. This is a good example of a function. It performs a specific task, pausing the screen, and has a name that describes what it does. Look through the Slide Show below to see the sample function.
 beginning of the function definition
1) This is the beginning of the function definition. This function will pause the screen.

This function echoes a message to the screen telling them to press return to continue
This function echoes a message to the screen telling them to press return to continue

Then the read command is used to pause the screen until the user types the Enter key.
Then the read command is used to pause the screen until the user types the Enter key. If the user happens to type in any data before pressing the Return key, it will be copied into the junk variable, which is not used for any purpose in the program, which is why it is called junk.

The read statement ends and then screen is cleared.
The read statement ends and then screen is cleared.

Removing a function

You can remove a function definition from memory by using the unset command. For example, to remove the pause_screen function, use the following command:
unset pause_screen

The next lesson demonstrates how to call a function within a shell script.

Shell Script Function Syntax - Quiz

Click the Quiz link below to test your knowledge of function purpose and syntax.
Shell Script Function Syntax - Quiz