The snippet library.
Small, practical examples. Search by task or narrow things down below.
Consume positional arguments one at a time
Bash Scripts: Check the argument count before accessing the first parameter with nounset enabled.
Bash Scripts✓ Tested
Convert a string to lowercase
Normalize a text value using Bash expansion.
lowercase✓ Tested
Convert a variable to uppercase
Bash Scripts: Double caret transforms matching lowercase characters in Bash 4 or later.
Bash Scripts✓ Tested
Copy an array independently
Bash Scripts: A new array assignment copies element values rather than creating a shared reference.
Bash Scripts✓ Tested
Count array elements
Bash Scripts: Count array elements with a small standalone Bash program.
Bash Scripts✓ Tested
Count down with a while loop
Bash Scripts: Count down with a while loop with a small standalone Bash program.
Bash Scripts✓ Tested
Count the elements in an array
Bash Scripts: The array length form counts assigned elements, not characters in the first value.
Bash Scripts✓ Tested
Declare a fixed configuration value
Bash Scripts: Declare a fixed configuration value with a small standalone Bash program.
Bash Scripts✓ Tested
Define and call a shell function
Bash Scripts: Define and call a shell function with a small standalone Bash program.
Bash Scripts✓ Tested
Detect an error inside a pipeline
Use pipefail to observe failure before the last pipeline command.
pipefail✓ Tested
Dispatch a command with case
Choose an action from a small set of supported commands.
case✓ Tested
Distinguish an empty input line from end of input
Bash Scripts: A successful read can return an empty value; its status indicates whether a line was read.
Bash Scripts✓ Tested