4.3. Functions

A function is simply a named command group. It is created by the use of the function keyword followed by the function name a pair of brackets and command group.

A function gets its own positional parameters derived from the options passed to it when called. The positional parameters from the parent calling level are not available and must be explicitly passed to the function if required.

Function calls may be nested to any depth but must not be recursive. Functions may be called within Section 4.4, “Command substitution”.

Example 4.4. Defining and using functions.

This example shows a function being defined and called with serveral times with differing parameters.

>function bar { echo $1; }
>function foo { echo -n $1; [ "$2" = "bar" ] && bar tree || echo "foo bar baz"; }
>foo baz
bazfoo bar baz
>foo baz bar
baztree
>foo baz tree
bazfoo bar baz
>