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.
Topic
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.
PHP-compatible str_pad() in pure Bash, when printf "%-10s" and %05d are enough, and why multibyte text breaks the alignment of your columns.
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 &.
PHP-style trim(), ltrim() and rtrim() in pure Bash with parameter expansion, plus why the popular read trick quietly removes your backslashes.
Bash's ${#var} counts characters, PHP's strlen() counts bytes. A clean strlen() function, a strbytes() companion, and the locale traps in between.
A strtolower() for Bash 4 and the stock macOS Bash 3.2, why ${var,,} depends on your locale, and when declare -l, tr, awk or nocasematch fit better.
A strtoupper() that works on Bash 4+ and macOS's Bash 3.2, plus ${var^^pattern} for uppercasing only some characters and declare -u for variables.
Get the directory part of a path in pure Bash, matching coreutils dirname on trailing slashes, bare names and the root, without starting a process.
Get the file name from a path with parameter expansion, matching coreutils basename on trailing slashes, "/" and suffixes, without starting a process.
A ucfirst() for Bash 4+ and macOS's Bash 3.2, what "first character" really means, and how to get "Hello" from "hELLO" or " hello".