Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

UNIX Shell function


May 23, 2021 UNIX Getting started


Table of contents


The shell function

Functions allow you to break down the overall functionality of a script into smaller logical sub-parts and then be called to perform their respective tasks when needed.

Using functions to perform repetitive tasks is a great way to create code reuse. Code reuse is an important part of the principles of modern object-oriented programming.

Shell functions are similar to subprograms and functions in other programming languages.

Create a function

To declare a function, simply use the following syntax:

    function_name () { 
       list of commands
    }

The name of the function_name is a function, and elsewhere in the script you can call it with the function name. The function name must be followed by parentheses, followed by parentheses, which contain a series of commands.

Example

Here's a simple example of using functions:

    #!/bin/sh

    # Define your function here
    Hello () {
       echo "Hello World"
    }

    # Invoke your function
    Hello

When you want to execute the script above, it produces the following results:

    $./test.sh
    Hello World
    $

The argument of the function is passed

You can define a function that can accept passed arguments when it is called. These parameters can be represented by $1, $2, etc.

Here's an example, we pass two parameters Zara and Ali, and then we capture and compile those parameters in a function.

    #!/bin/sh

    # Define your function here
    Hello () {
       echo "Hello World $1 $2"
    }

    # Invoke your function
    Hello Zara Ali

This results in the following:

    $./test.sh
    Hello World Zara Ali
    $

The function returns a value

If you execute an exit command from inside a function, you can terminate not only the execution of the function, but also the shell program that called the function.

If you just want to terminate the execution of the function, there is a way to jump out of the defined function.

Depending on the situation, you can use the return command to return any value from your function, as follows:

    return code

Code here can be anything you choose, but obviously, given the script as a whole, you should choose something meaningful or useful.

Example

The following function returns a value of 1:

    #!/bin/sh

    # Define your function here
    Hello () {
       echo "Hello World $1 $2"
       return 10
    }

    # Invoke your function
    Hello Zara Ali

    # Capture value returnd by last command
    ret=$?

    echo "Return value is $ret"

This results in the following:

    $./test.sh
    Hello World Zara Ali
    Return value is 10
    $

Nested functions

One of the more interesting features of functions is that they can call themselves as well as other functions. A function that calls itself is called a recursive function.

The following simple example shows the nesting of two functions:

    #!/bin/sh

    # Calling one function from another
    number_one () {
       echo "This is the first function speaking..."
       number_two
    }

    number_two () {
       echo "This is now the second function speaking..."
    }

    # Calling function one.
    number_one

This results in the following:

    This is the first function speaking...
    This is now the second function speaking...

Called from the Prompt function

You can place definitions of common functions in the file .profile so that you can get them when you load them and use them in the prompt command.

Alternatively, you can define multiple functions in a file, such as test.sh, and then execute the file in the current shell by typing the following:

    $. test.sh

Doing so allows test.sh function defined in the list to be read in and defined to the current shell, as follows:

    $ number_one
    This is the first function speaking...
    This is now the second function speaking...
    $

To remove the definition of a function from the shell, you can use .f command with the .f option. This is also the command used to remove the definition of a variable in the shell.

    $unset .f function_name