This page has been tested on macOS Catalina and Ubuntu 18.04 & 20.04
Functions are a way to re-use code. If you ever find yourself copying-and-pasting the same chunk of code more than twice, consider putting it inside a function and using that instead. These are essentially mini-scripts within a script.
Here’s the format of a function in Bash/Zsh:
my_function () {
# Write your code here
echo "Hello, World"
}
# Call your function like this
my_function
Hello, World
Notice the elements that make up a function:
my_function
) appears firstmy_function
BEFORE we had defined what it does with the brackets it would not have worked.Another format is like this:
function my_function {
# Write your code here
echo "Hello, World"
}
# Call your function like this
my_function
Hello, World
There’s no difference between these two format, it’s just personal preference as to which you use.
The advantage of a function is that you can call it as many times as you want without having to re-write the code each time:
my_function
my_function
my_function
Hello, World
Hello, World
Hello, World
Note that it’s much more useful if your functions have descriptive titles that reveal what they do, as opposed to generic ones:
# This is not a good name for a function
my_function
# This is much better as it tells you what it does
print_hello_world
The real power of functions is that they can be used to process data. In order to do this, they first need to be given some data and, secondly, they need to be able to do something with it:
my_function Hello World
the words “Hello” and “World” will be passed to the function called “my_function”.$1
is the name it gives to the first argument passed to it, $2
is the name for the second argument and so on.Here’s what it looks like in practice:
function my_function {
# Write your code here
echo $1
echo $2
}
# Call your function like this
my_function Hello World
Hello
World
A ‘return’ is the output of a function. It’s what the function returns to the rest of the script. It is created by the return
command and can be accessed in the main script by the $?
variable:
function my_function {
echo $1
echo $2
return 10
}
my_function Hello World
echo "What did you return? I returned the number $?"
Hello
World
What did you return? I returned the number 10
Note that a return can only be a number. In general, returns in Bash/Zsh are more commonly referred to as ‘exit codes’ and are mostly only useful for telling a user what happened during a function. For example, you might make your function return an ‘error code’ if something goes wrong: if a user tries to divide by 0 you might return the number 1, for example, which the user could then look up to see the meaning of and thus work out what went wrong. Usually, an exit code of 0 means that there were no errors.
Global variables are useful for making your script simpler: you can define a variable in one place and then use it anywhere. Local variables are useful when you want to avoid overwriting your other variables: if you only want to use a variable in one place in your script then only define it inside that one function to avoid confusion.
Global variables are created in the way we are used to creating variables:
# This variable is global, so it can be accessed inside functions
my_message="Hello, World"
function my_function {
echo $my_message
}
my_function
Hello, World
Local variables are create using the local
command:
# This variable is global
my_message="Hello, World"
function my_function {
# This variable is local
local my_message="Goodbye, World"
echo "Message inside function: $my_message"
}
my_function
echo "Message outside function: $my_message"
Message inside function: Goodbye, World
Message outside function: Hello, World
In this script we were able to create a variable called “my_message” inside the function that did not overwrite the variable “my_message” that had been created outside the function.