Go to the first, previous, next, last section, table of contents.


Functions

Shell functions are defined with the function reserved word or the special syntax `funcname ()'. Shell functions are read in and stored internally. Alias names are resolved when the function is read. Functions are executed like commands with the arguments passed as positional parameters. (See section Command Execution.)

Functions execute in the same process as the caller and share all files and present working directory with the caller. A trap on EXIT set inside a function is executed after the function completes in the environment of the caller.

The return builtin is used to return from function calls.

Function identifiers can be listed with the functions builtin. Functions can be undefined with the unfunction builtin.

Autoloading Functions

A function can be marked as undefined using the autoload builtin (or `functions -u' or `typeset -fu'). Such a function has no body. When the function is first executed, the fpath variable will be searched for a file with the same name as the function. The usual alias expansion during reading will be suppressed if the autoload builtin or its equivalent is given the option -U; this is recommended for the use of functions supplied with the zsh distribution. Thus to define functions for autoloading, a typical sequence is:

fpath=(~/myfuncs $fpath)
autoload myfunc1 myfunc2 ...

If the KSH_AUTOLOAD option is set, or the file contains only a simple definition of the function, the file's contents will be executed. It will normally define the function in question, but may also perform initialization: this is executed in the context of the function execution, and may therefore define local parameters. It is an error if the function is not defined by loading the file.

Otherwise, the function is defined such that its body is the complete contents of the file. This form allows the file to be used directly as an executable shell script. If processing of the file results in the function being re-defined, the function itself is not re-executed. To force the function to perform initialization and be called, the file should contain initialization code (which will be discarded) in addition to a complete function definition (which will be retained for subsequent calls to the function), and a call to the shell function at the end.

For example, suppose the autoload file func contains

func() { print This is func; }
print func is initialized

then `func; func' with KSH_AUTOLOAD set will produce both messages on the first call, and just the message `This is func' on the second and any subsequent calls. Without KSH_AUTOLOAD set, it will produce the initialization message on the first call, and the other message on the second and subsequent calls.

Special Functions

The following functions, if defined, have special meaning to the shell:

chpwd
Executed whenever the current working directory is changed.
periodic
If the parameter PERIOD is set, this function is executed every $PERIOD seconds, just before a prompt.
precmd
Executed before each prompt.
preexec
Executed just after a command has been read and is about to be executed. If the history mechanism is active, the string to be executed is passed as an argument.
TRAPNAL
If defined and non-null, this function will be executed whenever the shell catches a signal SIGNAL, where NAL is a signal name as specified for the kill builtin. The signal number will be passed as the first parameter to the function. If a function of this form is defined and null, the shell and processes spawned by it will ignore SIGNAL.
TRAPDEBUG
Executed after each command.
TRAPEXIT
Executed when the shell exits, or when the current function exits if defined inside a function.
TRAPZERR
Executed whenever a command has a non-zero exit status.

The functions beginning `TRAP' may alternatively be defined with the trap builtin: this may be preferable for some uses, as they are then run in the environment of the calling process, rather than in their own function environment.


Go to the first, previous, next, last section, table of contents.