Functions
Define and call
Functions behave like simple commands. Pass arguments as $1… inside.
greet() {
local who="${1:-world}"
printf 'Hello, %s\n' "$who"
}
greet Ada
Return status vs stdout
return sets the exit status (0–255). Print results to stdout; check status with $?.
is_file() {
[[ -f "$1" ]]
}
if is_file "notes.txt"; then echo yes; fi
Prefer locals
Without local, assignments affect the global scope and cause subtle bugs.
Keep functions focused
One job per function. Name them with verbs: backup_files, require_cmd.
require_cmd() {
command -v "$1" >/dev/null || {
echo "missing: $1" >&2
exit 127
}
}
Comments
One comment per signed-in account. Comments are saved with this page’s URL.