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: Shows how function is defined and called in bash

# Defining my_func function
my_func()
{
  echo "Function my_func is called"
  return 3
}

my_func # Calling my_func function
return_value=$?
echo "Return value of function = $return_value"

To call my_func() in shell script, we just have to write a function's name:

my_func

The my_func function has a return value as 3. The return value of a function is the exit status of a function. In the preceding example, the exit status of the my_func function is assigned to the return_value variable.

The result of running the preceding script is as follows:

Function my_func is called
Return value of function = 3

The return value of a function is what the return shell builtin is specified in its argument. If no return is used, then the exit code of the last command is executed in the function. In this example, the exit code will be the exit code of the echo command.

Passing parameters to functions

An argument to a function can be provided by specifying the first name of the function followed by space-separated arguments. A function in shell doesn't use parameters by its name but by positions; we can also say that the shell function takes positional parameters. Positional parameters are accessed by the variable names $1, $2, $3, $n, and so on, inside a function.

The length of arguments can be obtained using $#, a list of arguments passed can be fetched together using $@ or $*.

The following shell script explains how parameters are passed to the function in bash:

#!/bin/bash
# Filename: func_param.sh
# Description: How parameters to function is passed and accessed in bash

upper_case()
{
   if [ $# -eq 1 ]
   then
     echo $1 | tr '[a-z]' '[A-Z]'
   fi
}

upper_case hello
upper_case "Linux shell scripting"

The output of the preceding script is as follows:

HELLO
LINUX SHELL SCRIPTING

In the preceding shell script example, we called the upper_case() method twice with the hello and Linux shell scripting parameters. Both of them get converted to uppercase. In a similar way, other functions can be written to avoid writing repetitive work again and again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.107.193