The snippet library.
Small, practical examples. Search by task or narrow things down below.
Clamp a numeric setting to a valid range
Bash Scripts: Use arithmetic comparisons before applying a user-supplied bounded setting.
Bash Scripts✓ Tested
Format a numeric label with leading zeroes
Bash Scripts: Use a constant printf format string and supply values as separate arguments.
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
Parse digits with leading zeroes as decimal
Bash Scripts: The 10# prefix avoids Bash treating a leading zero as an octal notation.
Bash Scripts✓ Tested
Print text containing percent characters literally
Bash Scripts: Never use untrusted text as the printf format string.
Bash Scripts✓ Tested
Read fields captured by a Bash regular expression
Bash Scripts: BASH_REMATCH contains the full match followed by capture groups after a successful regex test.
Bash Scripts✓ Tested
Remove a trailing carriage return from a line
Bash Scripts: Use an ANSI-C quoted carriage return as the suffix pattern.
Bash Scripts✓ Tested
Set and clear a bit in a shell integer
Bash Scripts: Use shifts and bitwise operators for a small flag field.
Bash Scripts✓ Tested
Split a record while retaining its remainder
Bash Scripts: The final read variable receives the remainder of a delimited record.
Bash Scripts✓ Tested
Test numeric divisibility with a remainder
Bash Scripts: The modulo operator computes an integer remainder.
Bash Scripts✓ Tested