← Back to Bash Course | Chapter 8: Input/Output & Redirection | Lesson 1 of 13

Function Basics

In this page:

  1. Function Basics

Function Basics

A Bash function groups commands under a name using function_name() { ... }, letting you call the same logic repeatedly without duplicating code. Functions must be defined before they are called in the script. They're invoked just like any other command, by writing their name.

Note: The function keyword before the name is optional in Bash — greet() { ... } and function greet { ... } both work.

Example: Function Basics

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

greet
greet

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.