Shell Functions  «Prev  Next»
Lesson 2The purpose of functions
ObjectiveDefine the purpose of a function in a shell script.

Using functions within Shell Script

A function is a name given to a set of shell commands. You will take a subtask performed by your script and group the commands that perform that task into a function. Try to give functions descriptive names that identify the task they perform.

Advantages of using functions

Scripts that use functions with descriptive names are:
  1. Easier to read

Using functions in shell scripts has several advantages:
  1. Modularity: Functions allow you to break your script into smaller, more manageable pieces. This makes the code easier to understand and maintain, as each function can focus on a specific task or operation.
  2. Reusability: Functions can be called from multiple places within a script or from other scripts. This makes it easy to reuse code, reducing the need for code duplication and making your code more efficient and flexible.
  3. Abstraction: Functions can abstract away complex or repetitive tasks, making the code more concise and easier to read. This can be especially useful when the function is used to perform a task that involves multiple commands or complex logic.
  4. Parameterization: Functions can be defined with parameters, which allows them to be customized for different use cases. This can help to make the code more flexible and adaptable, without having to write multiple versions of the function.
  5. Encapsulation: Functions can encapsulate complex or critical operations, reducing the risk of error or damage to the system. This can help to ensure that critical tasks are performed consistently and without error.
  6. Portability: Functions can be written in a way that makes them portable across different Unix systems, making them useful for code that needs to be used on multiple systems.

By using functions in your shell scripts, you can make your code more efficient, maintainable, and flexible, and reduce the risk of errors or damage to the system.
Scripts with functions are easier to read if you give your functions descriptive names. If you call a function add_new_user, it’s easy to tell what that code does. If, instead, you see 25 lines of code that add a new user to a file, it’s not immediately obvious what those lines of code do.
  1. Faster to update
If you want to add a new feature to a script that uses functions, you can write a new function to perform the new task and add it into the existing script. Without functions, you must read through all the lines of the script to determine where to add new lines of code.
  1. Faster to debug

Easier to read

Because scripts with functions are easier to read, they are easier to debug. The faster you can read a script the easier it is to locate a problem. Since functions break a script down into logical subtasks, you can locate a bug by determining which task was being performed when the bug happened and then examining the corresponding function.
The next lesson explains proper function syntax.