How to Create and Call Functions in Bash
A quick guide on how to create and call functions in Bash.
A function is a block of reusable code that is used to perform some action. With functions, we get better modularity and a high degree of code reuse.
Bash provides some built-in functions such as echo
and read
, but we can also create our own functions.
Creating a Function in Bash
There are two ways we can create functions in Bash:
One way is to just use the function name, e.g:
functionName(){
// scope of function
}
Compact version:
functionName(){ echo "hello"; }
Another way is to declare a function using the function
keyword:
function functionName {
// scope of function
}
Compact version:
function functionName { echo "hello"; }
Notice how we don’t need the ()
when using the function
keyword to create a function.
Important points to note about Bash functions:
- The code between the curly braces
{}
is the function body and scope - When calling a function, we just use the function name from anywhere in the bash script
- The function must be defined before it can be used
- When using the compact version, the last command must have a semicolon
;
Example:
The following code creates a function which prints out “Hello World” to the console. The name of the function is called printHello:
#!/bin/bash
printHello(){
echo "Hello World!"
}
Calling a Function in Bash
How do we call the above function? All you need to do in your bash script is to write the name of the function and it will be called.
For example:
#!/bin/bash
printHello(){
echo "Hello World!"
}
# Call printHello function from anywhere in the script by writing the name
printHello
Output:
"Hello World"
Passing Arguments
The above function printHello()
doesn’t have any parameters. Anytime we call it, we get the output “Hello World”. But what if we wanted to create a more generic function? For example we can call the function with some argument and it will print what we send to it.
There are two ways of doing this.
First we can modify the printHello()
function to print the arguments that is passed to it:
For example:
#!/bin/bash
printAny(){
echo "Hello " $1
}
printAny World
printAny DevQa
printAny I love coding!
Output:
Hello World
Hello DevQA
Hello I
Notice how the third print statement printAny I love coding!
only outputted “Hello, I”.
This is because our function is designed to only take 1 parameter $1
. The word “I love coding!” is actually 3 parameters.
If we wanted to print it all we would need to put quotes around the text
For example:
#!/bin/bash
printAny(){
echo "Hello " $1
}
printAny "I love coding!"
Output:
Hello I love coding
Another example, we can pass in digits as well:
#!/bin/bash
add() {
result=$(($1 + $2))
echo "Result is: $result"
}
add 1 2
Output:
Result is: 3
Returning Values
Bash functions can also return values.
For example:
#!/bin/bash
add() {
result=$(($1 + $2))
}
add 1 2
echo "The sum is: "$result
Output:
The sum is: 3
Another way to return values from a function is to assign the result to a variable which can be used as and when needed.
For example:
#!/bin/bash
add () {
local result=$(($1 + $2))
echo "$result"
}
result="$(add 1 2)"
echo "The sum is: "$result
Output:
The sum is: 3