For historical reasons, there are two ways to define functions, the
syntax and the
syntax, described in the Commands later in this appendix. Shell functions are read in and stored internally. Alias
are resolved when the function is read. Functions are executed like commands with the arguments passed as positional parameters. (See Execution later in this appendix.)
Functions defined by the
syntax and called by name execute in the same process as the caller and share all files and present working directory with the caller. Traps caught by the caller are reset to their default action inside the function. A trap condition that is not caught or ignored by the function causes the function to terminate and the condition to be passed on to the caller. A trap on
set inside a function is executed in the environment of the caller after the function completes. Ordinarily,
are shared between the calling program and the function. However, the
special builtin command used within a function defines local variables whose scope includes the current function and all functions it calls. Errors within functions return control to the caller.
Functions defined with the
syntax and functions defined with the
syntax that are invoked with the
special built-in command are executed in the caller's environment and share all variables and traps with the caller. Errors within these function executions cause the script that contains them to abort.
The special built-in command
is used to return from function calls.
Function names can be listed with the
option of the
special built-in command. The text of functions, when available, will also be listed with
. Functions can be undefined with the
option of the
special built-in command.
Ordinarily, functions are unset when the shell executes a shell script. Functions that need to be defined across separate invocations of the shell should be placed in a directory and the
variable should contain the name of this directory. They may also be specified in the