Basic Script Functions - Shell Scripting

As you start writing more complex shell scripts, you’ll find yourself reusing parts of code that perform specific tasks. Sometimes it’s something simple, such as displaying a text message and retrieving an answer from the script users. Other times it’s a complicated calculation that’s used multiple times in your script as part of a larger process.

In each of these situations, it can get tiresome writing the same blocks of code over and over again in your script. It would be nice to just write the block of code once, then be able to refer to that block of code anywhere in your script without having to rewrite it.

The bash shell provides a feature allowing you to do just that. Functions are blocks of script code that you assign a name to, then reuse anywhere in your code. Anytime you need to use that block of code in your script, all you need to do is use the function name you assigned it (referred to as calling the function). This section describes how to create and use functions in your shell scripts.

Creating a function

There are two formats you can use to create functions in bash shell scripts. The first format uses the keyword function, along with the function name you assign to the block of code:

function name {
commands
}

The name attribute defines a unique name assigned to the function. Each function you define in your script must be assigned a unique name.

The commands are one or more bash shell commands that make up your function. When you call the function, the bash shell executes each of the commands in the order they appear in the function, just as in a normal script.

The second format for defining a function in a bash shell script more closely follows how functions are defined in other programming languages:

name() {
commands
}

The empty parentheses after the function name indicate that you’re defining a function. The same naming rules apply in this format as in the original shell script function format.

Using functions

To use a function in your script, specify the function name on a line, just as you would any other shell command:

$ cat test1
#!/bin/bash
# using a function in a script
function func1 {
echo "This is an example of a function"
}
count=1
while [ $count -le 5 ]
do
func1
count=$[ $count + 1 ]
done
echo "This is the end of the loop"
func1
echo "Now this is the end of the script"
$ ./test1
This is an example of a function
This is an example of a function
This is an example of a function
This is an example of a function
This is an example of a function
This is the end of the loop
This is an example of a function
Now this is the end of the script
$

Each time you reference the func1 function name, the bash shell returns to the func1 function definition and executes any commands you defined there.

The function definition doesn’t have to be the first thing in your shell script, but be careful. If you attempt to use a function before it’s defined, you’ll get an error message:

$ cat test2
#!/bin/bash
# using a function located in the middle of a script
count=1
echo "This line comes before the function definition"
function func1 {
echo "This is an example of a function"
}
while [ $count -le 5 ]
do
func1
count=$[ $count + 1 ]
done
echo "This is the end of the loop"
func2
echo "Now this is the end of the script"
function func2 {
echo "This is an example of a function"
}
$ ./test2
This line comes before the function definition
This is an example of a function
This is an example of a function
This is an example of a function
This is an example of a function
This is an example of a function
This is the end of the loop
./test2: func2: command not found
Now this is the end of the script
$

The first function, func1, was defined after a couple of statements in the script, which is perfectly fine. When the func1 function was used in the script, the shell knew where to find it.However, the script attempted to use the func2 function before it was defined. Since the func2 function wasn’t defined, when the script reached the place where I used it, it produced an error message.You also need to be careful about your function names. Remember, each function name must be unique, or you’ll have a problem. If you redefine a function, the new definition will override the original function definition, without producing any error messages:

$ cat test3
#!/bin/bash
# testing using a duplicate function name
function func1 {
echo "This is the first definition of the function name"
}
func1
function func1 {
echo "This is a repeat of the same function name"
}
func1
echo "This is the end of the script"
$ ./test3
This is the first definition of the function name
This is a repeat of the same function name
This is the end of the script
$

The original definition of the func1 function works fine, but after the second definition of the func1 function, any subsequent uses of the function use the second definition.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Shell Scripting Topics