Programming with the UNIX system shell


Functions provide a convenient and efficient means of coding and executing simple programs (more complex programs should remain as shell programs). Functions are similar to shell programs except for the fact that they are stored in memory and therefore, execute faster than a program. Another exception is that functions only operate in the current shell process.

Defining a function

There are two formats that can be used in defining a function:

Format 1

   name () { command; command; ... command; }
In this format, name is the name of the function. The parentheses is an indication to the shell that a function is being defined. The body of the function (which is delimited by the curly braces) contains the commands to be executed. Each command is separated by a semi-colon and a space. The last command ends with a semi-colon, and the curly braces are separated from the body of the function by a space.

Format 2

   name ()
   > {
   > command
   > command
   > command
   > }
In this format, name () is the same as in format 1. However, upon pressing the <RETRUN> key, a ``>'' prompt will replace your regular shell prompt. The body of the function is coded at this point, starting with the left curly brace. After the last command has been entered, the body of the function is closed with a right curly brace. It is not necessary to use semi-colons in this format.

Just as the exit statement is used within shell programs, the return statement is provided for use within functions. This statement will terminate the function, but not the shell program that called the function. The format of the return statement is:

   return n
where n is the return status of the function. If n is omitted, or if a return statement is not coded within the function, then the return status is that of the last command executed within the function.

Once the function has been defined, you can display it by using the shell set statement (without arguments) which displays all of your current environment variable settings. At the end of the variable list, any functions you have defined will be displayed.

If you find it necessary to remove a function during a session, the unset command can be used.

The format is:

   unset function
where function is the name of the function to be removed.

Executing a function

To execute a function, enter the name of the function at your regular shell prompt. Any arguments listed after the name of the function replace the positional parameters coded within the function (the same as in any other shell program).

After a function has executed, you can display the return status by issuing the following:

   echo $?


The following defines a function that displays login information for a particular user (notice that format 1 is used in this case):

   whoon () { who | grep $1; }
The next example searches for a file in the current directory. Notice that format 2 is used in this case. Also, the return statement is used. A return status of 1 indicates that the search did not find the file in question (a message is also displayed to that effect). A return status of 0 indicates that the file exists.
   isthere ()
   	if [ ! -f $1 ]
   		echo "$1 was not created"
   		return 1
   	return 0

Next topic: Debugging programs
Previous topic: Unconditional control statements: break and continue

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004