zsh - Functions

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

# Functions

The `function` reserved word is used to define shell functions. 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.

Functions execute in the same process as the caller and share all files and the 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.

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

`chpwd`
  Executed whenever the current working directory is changed.
`precmd`
  Executed before each prompt.
`periodic`
  If the parameter `PERIOD` is set, this function is executed every `PERIOD` seconds, just before a prompt.
`TRAPxxx`
  If defined and non-null, this function will be executed whenever the shell catches a signal `SIGxxx`, where xxx is a signal name as specified for the `kill` builtin (see section Shell Builtin Commands). In addition, `TRAPZERR` is executed whenever a command has a non-zero exit status, `TRAPDEBUG` is executed after each command, and `TRAPEXIT` is executed when the shell exits, or when the current function exits if defined inside a function. If a function of this form is defined and null, the shell and processes spawned by it will ignore `SIGxxx`.

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