Using functions and positional parameters

Similar to other programming languages, function is a way to write a set of actions once and use it multiple times. It makes the code modular and reusable.

The syntax of writing a function is as follows:

function function_name
 {
  # Common set of action to be done
 }

Here, function is a keyword to specify a function and function_name is the name of the function; we can also define a function in the following ways:

function_name()
{
  # Common set of action to be done
}

The actions written within curly braces are executed whenever a particular function is invoked.

Calling a function in bash

Consider the following shell script that defines the my_func()function:

#!/bin/bash # Filename: function_call.sh # Description: ...

Get Linux Shell Scripting Essentials 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.