1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Discuss in my forum

Functions and Subroutines in Bash Scripts

Methods to Re-use Code

By

Even though Bash is a Linux shell it has many of the capabilities needed to be a general purpose programming language, such as loops and conditional expressions. While-loops and For-loops are ways to execute a sequence of instructions multiple times. Functions are another method to save you from duplicating code. Functions are separate blocks of code that can be called any number of times from any other location of a Bash script, including from within a function. In other programming languages functions are also called subroutines.

Let start with simple example:

#!/bin/bash

function print_bold {
   echo "<b>$1</b>"
}

print_bold "batman"

In this script we first define a function print_bold, which takes the first pararmeter ($1) and prints it out enclosed with the html tags for bold.

After the function is defined, we call it with the statement:

print_bold "batman"

where batman is the argument (parameter) passed to the function.

You can run this script in the usual way with:

./test_script.sh

Assuming the filename of the script is test_script.sh and the current directory is the directory where the script is saved.

Note that the definition of a function has to come before it is used.

As in other places of a bash script you can have comments inside a function definition by starting the line with a "hash" (#), as in this example:

#!/bin/bash

function print_bold {
   # This function prints a string
   # bold tags around it.
   echo "<b>$1</b>"
}

print_bold "batman"

The line that starts with '#!' specifies which program should execute this script.

There is an alternative syntax to define a bash function:

print_bold() {
   # This function prints a string
   # bold tags around it.
   echo "<b>$1</b>"
}

Instead of using the function key word, you can define a function using parentheses () after the function name similar to the way functions are defined in Java or C++ code.

You can check if a parameter passed to a function is non-empty using the -z test:

print_bold() {
   if [ -z "$1" ]
   then
      echo "Argument is empty"
   else
      echo "<b>$1</b>"
   fi
}

See If-statements for more information about conditional expressions.

You can define global variables and access them from within a function definition as illustrated in this example:

PARAM1="em"

function print_bold {
   echo "<$PARAM1>$1</$PARAM1>"
}

The variable PARAM1 is assigned the value "em" before the function is defined. Then it used as part of the output string generated by the function.

However, you cannot directly access parameters passed from the command line If you want to access command line arguments inside a function you have to explicitly pass it in:

#!/bin/bash

PARAM1=$2
PARAM2=$1

function print_bold {
   echo "<$PARAM1>$1</$PARAM1>"
   echo "<$PARAM2>$1</$PARAM2>"
   echo "<$2>$1</$2>"
}

print_bold "batman" $3

When the subroutine (function) print_bold is called it receives two parameters: "batman" and "$3", which is the third command line argument. This value is then accessed from inside the function also with "$2" since it is the second argument pass to the function.

So if you call the script with:

./test_function02.sh b em it

You should the following output:

<em>batman</em>
<b>batman</b>
<it>batman</it>

You can also you for-loops, while-loops, and other flow control statements in function definitions.

There are various conventions and methods for passing arguments into Bash shell scripts from the command line.

  1. About.com
  2. Technology
  3. Linux
  4. Linux HowTos
  5. Bash How-To's
  6. Functions and Subroutines in Bash Scripts

©2014 About.com. All rights reserved.