Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

Functions · Bash Automation

Direct answer

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.

When to use this

Use this when organizing Bash code into functions.

First 30 seconds

1. Functions group logic; return status codes, echo data.

2. local variables.

3. Small and named clearly.

4. Test with bats.

5. Document arguments.

Operational verification

args → function → status/output

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
GNU Bash shell automation
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformGNU Bash shell automation · Automation implementations are tool and platform specific. Use the adjacent Domain for the technology being automated and this Domain for automation behavior.
Scope / prerequisiteUse this when organizing Bash code into functions.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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 return with values other than status codes.
Evidence references

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.

observation · 1
Key points
  • Arguments in $1, $2.
  • Use local variables.
  • Return status codes, print data.
Key checks
  • 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.

Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docGNU Bash shell automation current

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%
Depth when you need it

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.

Confirmation

Confirm action

Continue?