Linux Basics: Build Small Bash Functions
When a script repeats a small operation, give that operation a name. Bash functions help organize arguments, temporary variables, and results. These examples build on a first script rather than requiring a framework.
These examples were tested on Linux with GNU Bash 5.2.37. Use Bash rather than sh; some syntax is Bash-specific. No packages or administrator access are needed.
Prepare a scratch directory
You need Bash, a text editor, and the mktemp, rm, and rmdir utilities. Keep using the same terminal. The guards stop the shell if setup fails. Save each named source file in this directory before running its command. Calling bash explicitly means you do not need to change executable permissions.
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 11. Pass arguments to a helper
Save this as function-arguments.sh:
#!/usr/bin/env bash
greet() {
printf 'Hello, %s!\n' "$1"
}
greet 'Ada Lovelace'
greet 'Grace Hopper'Run it:
bash function-arguments.shOutput:
Hello, Ada Lovelace!
Hello, Grace Hopper!Define the function before calling it. Arguments follow its name, without call parentheses. Inside the function, $1 is its first argument. This small helper expects one argument; production helpers should define how they handle missing input.
2. Keep temporary names local
Save this as function-local.sh:
#!/usr/bin/env bash
label='outside'
show_label() {
local label='inside'
printf 'Function: %s\n' "$label"
}
show_label
printf 'Caller: %s\n' "$label"Run it:
bash function-local.shOutput:
Function: inside
Caller: outsideA function normally shares variables with its caller. local protects the caller’s variable here. Bash uses dynamic scope: functions called by this helper can also see its local variable. Use local names deliberately rather than assuming functions automatically isolate state.
3. Separate returned text from status
Save this as function-result.sh:
#!/usr/bin/env bash
describe_mode() {
case "$1" in
fast) printf 'Quick preview\n'; return 0 ;;
safe) printf 'Checked preview\n'; return 0 ;;
*) return 2 ;;
esac
}
if description=$(describe_mode safe); then
printf 'Description: %s\n' "$description"
fi
if description=$(describe_mode unknown); then
printf 'Description: %s\n' "$description"
else
status=$?
printf 'Unknown mode, status=%s\n' "$status"
fiRun it:
bash function-result.shOutput:
Description: Checked preview
Unknown mode, status=2Use standard output for text and return for a numeric success or failure status. Command substitution captures text and removes trailing newlines. An assignment containing this substitution takes the command’s status. Save $? immediately in the failure branch. Keep diagnostic messages on standard error if the caller captures standard output.
Clean up
In the same terminal, remove only the example files, return to your original directory, and remove the empty scratch directory:
rm -- function-arguments.sh function-local.sh function-result.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirReference
The GNU Bash manual: Shell Functions documents the syntax used here.
Acknowledgements
This post was created with assistance from AI.