Functions

Syntax

Definition

function_name() {
    command
}

Invocation

function_name [parameters]

Discussion

  • command may be a single command or multiple commands, which are either placed on different lines or divided by ; on the same line.
  • We advise to always quote the parameters passed to a function.
  • An alternate syntax is possible:
    function function_name() {
        command
    }
    When the alternate syntax with the reserved word function is used, then the () after the function_name can be omitted.

Example

#!/usr/bin/env bash

abort() {
  echo "${1}"
  exit 1
}

(( $# == 0 )) && abort "Error: No parameter passed."

echo "Passed parameter: '${1}'"

2020–07–19