AcademyThe Automator's Grimoire: Scripting WizardryCircle 3: Rituals (Functions)

Functions & Arguments

As your scripts grow, you'll want to organize your code into reusable blocks.

Positional Arguments

When a script or function is executed, it can receive arguments.

  • $1, $2, $3: The first, second, and third arguments.
  • $@: All arguments as a list.
  • $#: The number of arguments passed.

Writing Functions

Bash functions look a lot like functions in other languages, but they don't have a specific return keyword for returning strings (they return exit codes instead). To "return" data, you echo it.

#!/bin/bash

# Define the function
greet() {
    local NAME=$1
    echo "Hello $NAME, welcome to the server!"
}

# Call the function
greet "Alice"
greet "Bob"
booting...

Mission Control

Write a function

Expected Command

cat func.sh

Call the function