PHP String Functions in Bash: A Cheat Sheet
The index to the Bash function library: every PHP string function with its pure-Bash equivalent, short tested snippets, and when to use awk or sed instead.
Category
The pure Bash function library (PHP-style helpers written in Bash) and practical shell scripting techniques.
The index to the Bash function library: every PHP string function with its pure-Bash equivalent, short tested snippets, and when to use awk or sed instead.
Writing Bash builtins in C: the .def format, compiling into bash vs enable -f, what my old awie-bash code got wrong, and a tested loadable port with timings.
Temp files and stale locks left behind by failed scripts are avoidable. Tested trap patterns for EXIT, INT, TERM and ERR, plus mkdir and flock locks.
A tested getopts skeleton for Bash scripts: flags, options with values, required options, error handling, and a manual loop for --long options.
set -euo pipefail is good advice, but each option has rules about when it fires. Here is what each one does and the cases that surprise people.
while IFS= read -r line is the correct loop, but the last line, pipes, commands that read stdin and Windows line endings each have a trap. Here they are, tested.
PHP-compatible str_pad() in pure Bash, when printf "%-10s" and %05d are enough, and why multibyte text breaks the alignment of your columns.
A correct in_array() for Bash, an associative-array set for many lookups, and a demonstration of why [[ "${arr[*]}" =~ value ]] gives false positives.
PHP's explode() and implode() in Bash: read -ra and mapfile -d for splitting, "${arr[*]}" for joining, and functions that cope with multi-character delimiters.
Replace text in a Bash string without sed: ${s//search/replace}, anchored replacements, why you must quote the search, and what Bash 5.2 did to &.