Functions

Functions: Functions: Naming a Job, Local Variables and Getting a Value Back, One Script, Several Files: source.

Updated

  1. Functions: Naming a Job (Journeyman): A function keeps a few commands under one name, so a script says the job's name instead of repeating its lines: define it with name() { }, call it by its name like any command, and hand it arguments, which it reads as $1, $# and "$@".
  2. Local Variables and Getting a Value Back (Journeyman): local keeps a function's variables to itself, so it can't change the script's by accident; a function hands back a result by printing it with echo, which the caller catches with $( ), and answers yes or no with return, which if can test.
  3. One Script, Several Files: source (Journeyman): A library is a file of functions that several scripts share: each script loads it with source, and finds it from any folder with here=$(dirname "${BASH_SOURCE[0]}"), the folder the script itself is saved in.