← Back to Bash Course | Chapter 5: Functions | Lesson 2 of 11

Defining and Calling Functions

A function is a named block of commands you define once and can then run again and again just by writing its name, like giving a mini command its own name.

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

bash
#!/bin/bash
greet() {
    echo "Hello from the greet function!"
}
greet

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

bash
#!/bin/bash
show_divider() {
    echo "------------------------"
}
show_divider
echo "Section one"
show_divider
echo "Section two"
show_divider

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

bash
#!/bin/bash
say_hi() {
    echo "Hi there!"
}
say_hi

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

bash
#!/bin/bash
function farewell {
    echo "Goodbye!"
}
farewell
Common Mistakes
  1. Putting parentheses with parameters in the definition, like greet(name) { ... }; Bash function definitions never declare parameters in the parentheses -- they are just name() { ... } or function name { ... }, empty parens either way.
  2. 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.
  3. 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.
Chapter Summary
  • Define with name() { commands; } or function 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.