Functions

As with other programming languages, shell programs also support functions. A function is a piece of a shell program that performs a particular process that can be used more than once in the shell program. Writing a function helps you write shell programs without duplication of code.

The following is the format of a function in pdksh and bash for function definition:

func(){
   Statements
}

You can call a function as follows:

func param1 param2 param3
					

The parameters param1, param2, and so on are optional. You can also pass the parameters as a single string--for example, $@. A function can parse the parameters as if they were positional parameters passed to a shell program.

The following example is a function that displays the name ...

Get Red Hat® Linux® 7 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.