Using Functions

Functions are defined as follows:

						name () { list ; }

Here, name is the name of the function and list is a list of commands. The list of commands, list, is referred to as the body of the function. The parentheses, ( and ), that follow name are required.

The job of a function is to bind name to list, so that whenever name is specified list is executed. When a function is defined, list is not executed; the shell parses list to ensure that there are no syntax errors and stores name in its list of commands. The following example illustrates a basic function definition:

lsl() { ls –l ; }

Here you define the function lsl and specify list as ls –l.

An alternative form of function definition is available in ksh, bash, and zsh:

 function ...

Get Sams Teach Yourself Shell Programming in 24 Hours, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.