Text-only Table of Contents (frame/ no frame)
(23) Functions Previous Top Next

Shell Functions

All but the earliest versions of sh allow you define shell functions, which are visible only to the shell script and can be used like any other command. Shell functions take precedence over external commands if the same name is used. Functions execute in the same process as the caller, and must be defined before use (appear earlier in the file). They allow a script to be broken into maintainable chunks, and encourage code reuse between scripts.

Defining functions

identifier() { list; }
POSIX syntax for shell functions. Such functions do not restrict scope of variables or signal traps. The identifier follows the rules for variable names, but uses a separate namespace.
function identifier { list; }
Ksh and bash optional syntax for defining a function. These functions may define local variables and local signal traps and so can more easily avoid side effects and be reused by multiple scripts.

A function may read or modify any shell variable that exists in the calling script. Such variables are global.

(ksh and bash only) Functions may also declare local variables in the function using typeset or declare. Local variables are visible to the current function and any functions called by it.

return [n], exit [n]
Return from a function with the given value, or exit the whole script with the given value.
Without a return, the function returns when it reaches the end, and the value is the exit status of the last command it ran.

Example:

die()
{
   # Print an error message and exit with given status
   # call as: die status "message" ["message" ...]
   exitstat=$1; shift
   for i in "$@"; do
      print -R "$i"
   done
   exit $exitstat
}

Calling functions.

Functions are called like any other command. The output may be redirected independantly of the script, and arguments passed to the function. Shell option flags like -x are unset in a function - you must explicitly set them in each function to trace the execution. Shell functions may even be backgrounded and run asynchronously, or run as coprocesses (ksh).

Example:

[ -w $filename ] || \
  die 1 "$file not writeable" "check permissions"

Example: Backgrounded function call. ex12 display, text

Example:

vprint()
{
   # Print or not depending on global "$verbosity"
   # Change the verbosity with a single variable.
   # Arg. 1 is the level for this message.
   level=$1; shift
   if [[ $level -le $verbosity ]]; then
      print -R $*
   fi
}

verbosity=2
vprint 1 This message will appear
vprint 3 This only appears if verbosity is 3 or higher

Reuseable functions

By using only command line arguments, not global variables, and taking care to minimise the side effects of functions, they can be made reusable by multiple scripts. Typically they would be placed in a separate file and read with the "." operator.

Functions may generate output to stdout, stderr, or any other file or filehandle. Messages to stdout may be captured by command substitution (`myfunction`, which provides another way for a function to return information to the calling script. Beware of side-effects (and reducing reusability) in functions which perform I/O.

Previous Top Next


functions.src  last modified Mar 11, 2005 Introduction Table of Contents
(frame/no frame)
Printable
(single file)
© Dartmouth College