Chapter 19. Shell functions

All the code in the scripts so far in this book has been executed from top to bottom. This approach is OK, but you may have noticed that some segments of the code we have covered in our examples has been duplicated further on in the same script.

The shell allows you to group a set of commands or statements into a reusable block. These blocks are called shell functions.

In this chapter we will cover:

  • defining functions;

  • using functions from within a script;

  • using functions from a functions file; and

  • function examples.

A function is made up of two parts:

The function label The function body 

The label is the function name and the body is the set of commands inside the function. The label name should really be unique; if ...

Get Linux and Unix Shell Programming 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.