Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Loops and Functions  ›  Lesson

Functions: Named Steps

Functions 14 minA function is a named block of commands, defined before it is called, that receives its own positional arguments and reports an exit status with return, while any data it produces is printed and captured.

Functions: Named Steps

backup.sh now prints time-stamped messages in six places, each a copy of the same printf line, and works out an archive name in two places. When you want to change the time format, you have six lines to find. A function gives a block of commands a name, so it is written once and called anywhere.

The rule

Define before calling; arguments arrive as $1, $2; return only reports status.

log() {
    printf '[%s] %s\n' "$(date +%T)" "$*" >&2
}

slugify() {
    local base="${1%/}"
    base="${base##*/}"
    echo "${base// /_}"
}

log "Starting backup"
name="$(slugify "$HOME/My Notes")"      # My_Notes
log "Archive name: $name"
  • Define with name() { commands; }. Bash reads top to bottom, so the definition must come before the first call.
  • Call it like any command: its name, then its arguments.
  • Inside, $1, $2, "$@" and $# are the function's own arguments, not the script's. ($0 is still the script name.)
  • return N ends the function with exit status N (0 to 255), so functions work with if, && and ||. Without it, the status is that of the last command inside.
  • To hand back data, the function prints it and the caller captures it with $( ). return "My_Notes" is an error: return only takes a number.
is_backup() { [[ "$1" == *.tar.gz ]]; }      # status is the test's status
if is_backup "$file"; then log "Found $file"; fi

The shortcut

Treat a function as a tiny script inside your script: it has its own $1 and $#, reports success with its exit status, and "returns" text by printing it. So ask of any function: does the caller need a yes/no, or a value? Yes/no means exit status; a value means echo plus $( ).

name="$(slugify "/home/asha/My Notes/")"Step 1 of 4
  1. The call

    slugify runs with one argument, so inside it $1 is /home/asha/My Notes/.

Drills

Answer fast — trust the rule.

quick drill

A script calls hello on line 3 and defines hello() { echo hi; } on line 10. What happens on line 3?

quick drill

After add() { echo $(( $1 + $2 )); }, what is in x after x=$(add 2 3)?

quick drill

What happens when a function runs return "hello"?

quick drill

A script is run with 4 arguments and calls k x y z, where k() { echo $#; }. What prints?

Remember

  • Define before calling; arguments arrive as $1, $2; return only reports status.
  • A function is a tiny script: yes/no through exit status, values through echo and $( ).
  • Trap: return "text" is an error; print the text instead.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTwo helpers for backup.sh

Write two functions for backup.sh: log, which prints its message with a time stamp to standard error, and slugify, which turns a path such as /home/asha/My Notes into My_Notes. Show how each is called, and explain how arguments reach a function and how slugify gets its result back to the caller.