The snippet library.
Small, practical examples. Search by task or narrow things down below.
Identify whether a name resolves to a builtin
Bash Scripts: type -t reports the command kind without printing a machine-specific path.
Bash Scripts✓ Tested
Increase an integer variable
Bash Scripts: Increase an integer variable with a small standalone Bash program.
Bash Scripts✓ Tested
Increment a counter without an unexpected errexit
Bash Scripts: An assignment avoids the zero-result exit status of a standalone arithmetic post-increment.
Bash Scripts✓ Tested
Inspect every command status in a pipeline
Bash Scripts: Copy PIPESTATUS immediately before another command overwrites it.
Bash Scripts✓ Tested
Isolate variable changes in a subshell
Bash Scripts: Parentheses create a subshell whose variable assignments do not change the parent.
Bash Scripts✓ Tested
Join array items with a one-character separator
Bash Scripts: A local IFS controls the separator used by the quoted star expansion.
Bash Scripts✓ Tested
Keep a function variable local
Bash Scripts: Local prevents an ordinary function assignment from replacing a caller variable.
Bash Scripts✓ Tested
Keep loop state by avoiding a pipeline subshell
Bash Scripts: Redirect input into a while loop so updates to a counter remain in the current shell.
Bash Scripts✓ Tested
Limit a beginner function variable to local scope
Bash Scripts: Limit a beginner function variable to local scope with a small standalone Bash program.
Bash Scripts✓ Tested
Load functions from a trusted local script
Bash Scripts: Source executes a file in the current shell so its function definitions remain available.
Bash Scripts✓ Tested
Look up values in an associative array
Use named keys for a small in-memory configuration table.
dictionary✓ Tested
Loop over an array safely
Preserve spaces in array elements while iterating.
array✓ Tested