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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.