Use this when organizing Bash code into functions.
Functions · Bash Automation
Bash functions group reusable commands, defined as name() { ...; }, with arguments in $1, $2 and local variables declared with local. They return a status code, not data.
1. Functions group logic; return status codes, echo data.
2. local variables.
3. Small and named clearly.
4. Test with bats.
5. Document arguments.
args → function → status/output
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
The negative fixture should deliberately exercise risks such as unquoted expansion, globbing, pipeline status loss, subshell scope, set -e misconceptions, signal races, temporary-file leakage and destructive path ambiguity, choosing the cases most relevant to parameter/return contracts, scope, side effects, exceptions/exit behavior and unit-testable boundaries rather than hidden global state.
Step 2 Common mistakes Narrow the fault
- Variables not declared
local, leaking into the script. - Using
returnwith values other than status codes.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Arguments in $1, $2.
- Use local variables.
- Return status codes, print data.
declare -f <func>— function body- local variables inside functions — scope
- return codes — status
ReferenceWhy it matters1 section · depth on demand
What it is
Bash functions group commands under a name, take arguments as $1, $2… and return a status code (0 = success).
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Bash functions group commands under a name so a script can reuse them and stay readable. They take positional arguments ($1, $2), return an exit status, and can output text for the caller to capture.
log() { printf '%s %s\n' "$(date -Is)" "$*" >&2; }
backup_device() {
local host=$1 dest=$2
log "backing up $host"
scp -q "backup@${host}:running-config" "${dest}/${host}.cfg" || return 1
}
for h in $(cat hosts.txt); do
backup_device "$h" /srv/backups || log "FAILED: $h"
done
Use local for variables inside functions (otherwise they are global), quote all expansions, and send diagnostic messages to stderr so stdout stays clean for data.
Example
log() { printf '%s %s\n' "$(date -Is)" "$*" >&2; }
backup() { local host=$1; scp "$host:/etc/config" "backups/$host.cfg"; }
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Bash Automation > Functions. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.gnu.org/software/bash/manual/bash.html · confidence 94%Deep reference
Healthy vs suspicious evidence
Output interpretation
For parameter/return contracts, scope, side effects, exceptions/exit behavior and unit-testable boundaries rather than hidden global state, prefer typed fields and stable IDs over human presentation text.