Using Functions! Shell Scripting Part 5

Published on September 15, 2022 at 1:02 am by LEW

Introduction

Today we will take a look at how to use functions in bash scripts. This is important, because functions in bash perform a little differently than in other programming languages.

It is important to understand the differences, to avoid hours of frustration (something I have had some experience with).

In this post I will first cover bash functions and what makes them unique and different from functions in other languages. Then will talk about how to use functions and how to get around some limitations.

After that I have a short script for review.

Two forms of Functions

One of the first things to know about bash script functions is that there are two basic forms.

func_name () {
<some code>
}

function func_name {
<some code>
}

These are purely aesthetic and do not make any difference whatsoever to how a function works in bash. In the first form, nothing goes inside the parenthesis. You can use whichever one you want (just be consistent thought your code wit the type used), as it makes zero difference to how the function works.

Where to Place a Function

In bash scripts all functions must physically occur before they are called. In general my practice is to define all functions close to the start of a script, before any other coding.

Passing Arguments to a Function

Arguments are passed to a function just like they are passed to a bash script(see previous post). When you call the function, you list the arguments on the same line immediately after the function call. Then they will be available within the function using $1, $2, $3, etc.

Return values from a Function

This is where things get a little dicey. Technically functions in bash do not return anything. So there are generally three methods to get data back from a function. Each comes with its own set of possible issues if you are not careful in your coding.

Script Example

The following script is simple, but does demonstrate all of the above.

#!/bin/bash

# Name: function_man          Version: 1
# Author: RetiredTechie       Date: 14 SEP 2022
#
# Description: Using Functions in BASH

# Bash functions can be one of two forms. The form does not matter so use
# what works best for you. Functions must also be located in the script
# before there are any calls. Note in form one the parenthesis are
# decoration and noting will go inside them.

fun_form_one () {

    echo This is Function Type One
    let "ADDRTN=$(($1+$2))"
    return $ADDRTN

}

function fun_form_two {

    echo $(($1 + $2))

}

# Functions - Passing Arguments, and returning Numeric Values
# Using function return and Global variable

fun_form_one 5 3

echo function return value set to: $?

echo Return Value Global Variable is: $ADDRTN

# Using command substitution in fun_form_two

echo Substitution Value is: $(fun_form_two 5 3)

exit

Script Review

Close to the top of the script we define two functions, one in each form. These are ingeniously named fun_form_one and fun_form_two.

When we call fun_form_one, we provide two in line arguments (5 and 3). The function tells us it is a type one, then adds the two numbers together, and stores them in a global variable (ADDRTN). Finally, it sets the return value equal to ADDRTN.

The “This is Function Type One” gets echoed to the screen immediately from within the function. Then from the main script we print out both the function return value and the global variable, which should both equal 8.

In the second function, we have it setup to echo the value of adding the two arguments together. Back in the main program we have another echo statement where we have dropped our function call into. This is a simple example of command substitution.

Conclusion

In this post we talked about basic functions in bash scripts and how they differ form what is common in other pro0gramiung languages.  We also talked about how to get data into and out of bash functions.

Next time around we will discuss variable scope in bash scripts.

Shell or Terminal? Shell Scripting – Part 1

Simple Script Example! Shell Scripting – Part 2

Command Line Arguments! Shell Scripting Part 3

User Input! Shell Scripting Part 4

Using Functions! Shell Scripting Part 5

Variable Odds and Ends! Shell Scripting Part 6

Add New Comment

Your email address will not be published. Required fields are marked *