Revision [1354]

This is an old revision of SyntaxFunction made by DavidLee on 2010-05-02 09:15:58.

 

Functions


Functions can be declared in two forms, with or without the keyword "function". These are the equivalent to the forms for ksh.
In both forms the arguments to the functions are passed as positional parameters ($1 .. $N)



name () { list; }


This declares a function "name". The body of the function when executed runs in the same context as the parent shell, with the exception of Positional Parameters ($1 ... $N).
This means that variables assigned within the function have a "side effect" of writing into the global environment for the current shell.

Example

setit () 
{
	 A=$1 ;
}

A="no value"
echo $A
setit value
echo $A


Result
no value
value


function name () { list; }


This declares a function "name". The body of the function when executed runs in the a child shell context. Arguments are passed as positional parameters ($1 .. $N)
This means that variables assigned within the function *do not* have a "side effect" of writing into the global environment for the current shell.

Example

function setit () 
{
	 A=$1 ;
}

A="no value"
echo $A
setit value
echo $A


Result
no value
no value


Return Value


The Return value of a function is the return value of the last command, or the argument of the return function.

Example
foo ()
{  
	true ; 
}

foo && echo Is True


Result
Is True


Example

foo ()
{
   return 2 ;
}

foo
echo $?


Result
2









CoreSyntax
Commands





There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki