Functions

Creating a function is fairly easy. They may be written in two different formats:

function_name () {
<commands>
}

or

function function_name {
<commands>
}

A few points to note:

  • Either of the above methods of specifying a function is valid. Both operate the same and there is no advantage or disadvantage to one over the other. It’s really just personal preference.
  • In other programming languages it is common to have arguments passed to the function listed inside the brackets (). In Bash they are there only for decoration and you never put anything inside them. We pass arguments directly after the call of function and within the function definition, we access the arguments by $1, $2, etc.
  • The function definition ( the actual function itself) must appear in the script before any calls to the function.

 

Arguments

As we discussed before, We pass arguments directly after the call of function and within the function definition, we access the arguments by $1, $2, etc.

argument_test.sh
#!/bin/bash

# Passing arguments to a function



print_something () {

echo Hello $1

}

print_something Mars

print_something Jupiter

If we call the script

./argument_test.sh

Hello Mars

Hello Jupiter
Return values

Most other programming languages have the concept of a return value for functions, a means for the function to send data back to the original calling location. Bash functions don’t allow us to do this. They do however allow us to set a return status. Similar to how a program or command exits with an exit status which indicates whether it succeeded or not. We use the keyword return to indicate a return status, and we can use $? to call this return value. Note that the return value must be a numeric value.

return_status_example.sh

#!/bin/bash

# Setting a return status for a function

print_something () {

echo Hello $1

return 5

}



print_something Mars

print_something Jupiter

echo The previous function has a return value of $?

Let’s break it down

  • Line 6 – The return status doesn’t have to be hardcoded. It may be a variable
  • Line 11 – Remember that the variable $? contains the return status of the previously run command or function.

 

Variable scope

Scope refers to which parts of a script can see which variables. By default a variable is global. This means that it is visible everywhere in the script. We may also create a variable as a local variable. When we create a local variable within a function, it is only visible within that function. To do that we use the keyword local in front of the variable the first time we set it’s value.

Best Practice: Always use local variables within functions. Use global variables as a last resort and consider if there is a better way to do it before using them.

Overriding commands

It is possible to name a function as the same name as a command you would normally use on the command line. This allows us to create a wrapper. eg. Maybe every time we call the command ls in our script, what we actually want is ls -lh. We could do the following:

#!/bin/bash
# Create a wrapper around the command ls
ls () {
command ls -lh
}
ls
  • Command key word in the line 4 is mandatory while the function can cause endless loop without it.

It’s easy to forget the command keyword and end up in an endless loop. If you encounter this then you can cancel the script from running by pressing the keys CTRL c