Defining and Calling Functions
In this page:
Defining a Simple Function
A function is defined with name() { commands }, where the curly braces wrap the block of commands that make up the function's body. The function keyword can optionally precede the name as an alternative style, but is not required in Bash.
Example: Defining a Simple Function
#!/bin/bash
greet() {
echo "Hello from the greet function!"
}
greet
Login to try C/C++/Java/PHP code in the editor
Calling a Function Multiple Times
Once defined, a function can be called as many times as needed throughout the rest of the script, exactly like any built-in command. This is the core benefit of functions: writing the logic once and reusing it wherever it's needed.
Example: Calling a Function Multiple Times
#!/bin/bash
show_divider() {
echo "------------------------"
}
show_divider
echo "Section one"
show_divider
echo "Section two"
show_divider
Login to try C/C++/Java/PHP code in the editor
Definitions Must Come Before Use
Bash reads and executes a script top to bottom, so a function must already be defined by the time the script reaches a line that calls it. Calling a function before its definition appears results in a 'command not found' error.
Warning: It's common practice to define all functions near the top of a script, followed by the main sequence of calls at the bottom.
Example: Definitions Must Come Before Use
#!/bin/bash
say_hi() {
echo "Hi there!"
}
say_hi
Login to try C/C++/Java/PHP code in the editor
The function Keyword Alternative
Bash also accepts function name { commands; } as an alternative syntax to name() { commands; }; both define a function identically. The parenthesis-based form is more portable to other POSIX-compatible shells, while function is a Bash/ksh-style extension.
Example: The function Keyword Alternative
#!/bin/bash
function farewell {
echo "Goodbye!"
}
farewell
Login to try C/C++/Java/PHP code in the editor
- Putting parentheses with parameters in the definition, like
greet(name) { ... }; Bash function definitions never declare parameters in the parentheses -- they are justname() { ... }orfunction name { ... }, empty parens either way. - Forgetting that a function must be defined (appear earlier in the script) before the point where it's called -- Bash executes top to bottom and doesn't hoist function definitions like some other languages.
- Calling a function with parentheses like other languages, e.g.
greet(); in Bash you call it just by name with arguments separated by spaces,greet Ada, without any parentheses at the call site.
- Define with
name() { commands; }orfunction name { commands; }-- both are equivalent in Bash. - Call a function exactly like any other command: just its name, optionally followed by space-separated arguments.
- Functions must be defined before they are called in the script's execution order.
- A function's body is just a group of commands; it does not require any special return type declaration.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: