Korn shell functions

The function reserved word defines shell functions. The shell reads and stores functions internally. Alias names are resolved when the function is read. The shell executes functions in the same manner as commands, with the arguments passed as positional parameters.

The Korn shell or POSIX shell executes functions in the environment from which functions are invoked. All of the following are shared by the function and the invoking script, and side effects can be produced:

  • Variable values and attributes (unless you use typeset command within the function to declare a local variable)
  • Working directory
  • Aliases, function definitions, and attributes
  • Special parameter $
  • Open files

The following are not shared between the function and the invoking script, and there are no side effects:

  • Positional parameters
  • Special parameter #
  • Variables in a variable assignment list when the function is invoked
  • Variables declared using typeset command within the function
  • Options
  • Traps. However, signals ignored by the invoking script will also be ignored by the function.
Note: In earlier versions of the Korn shell, traps other than EXIT and ERR were shared by the function as well as the invoking script.

If trap on 0 or EXIT is executed inside the body of a function, then the action is executed after the function completes, in the environment that called the function. If the trap is executed outside the body of a function, then the action is executed upon exit from the Korn shell. In earlier versions of the Korn shell, no trap on 0 or EXIT outside the body of a function was executed upon exit from the function.

When a function is executed, it has the same syntax-error and variable-assignment properties described in Korn shell or POSIX shell built-in commands.

The compound command is executed whenever the function name is specified as the name of a simple command. The operands to the command temporarily will become the positional parameters during the execution of the compound command. The special parameter # will also change to reflect the number of operands. The special parameter 0 will not change.

The return special command is used to return from function calls. Errors within functions return control to the caller.

Function identifiers are listed with the -f or +f option of the typeset special command. The -f option also lists the text of functions. Functions are undefined with the -f option of the unset special command.

Ordinarily, functions are unset when the shell executes a shell script. The -xf option of the typeset special command allows a function to be exported to scripts that are executed without a separate invocation of the shell. Functions that must be defined across separate invocations of the shell should be specified in the ENV file with the -xf option of the typeset special command.

The exit status of a function definition is zero if the function was not successfully declared. Otherwise, it will be greater than zero. The exit status of a function invocation is the exit status of the most recent command executed by the function.