trim() in Bash: Strip Leading and Trailing Whitespace
PHP-style trim(), ltrim() and rtrim() in pure Bash with parameter expansion, plus why the popular read trick quietly removes your backslashes.
Category
The pure Bash function library (PHP-style helpers written in Bash) and practical shell scripting techniques.
PHP-style trim(), ltrim() and rtrim() in pure Bash with parameter expansion, plus why the popular read trick quietly removes your backslashes.
Find processes by their full command line in pure Bash by reading /proc/PID/cmdline, with the NUL bytes handled properly and the 2009 bugs fixed.
Test whether a directory is empty in pure Bash with nullglob and dotglob, handle hidden and unreadable directories, and know when find -quit is faster.
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.
Check whether a string contains a substring regardless of case, with a Bash 4 version, a Bash 3.2 fallback using nocasematch, and a PHP-style stristr().
Test whether a string contains a substring in pure Bash, or get the rest of the string from the first match the way PHP's strstr() does.
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.
A yes/no confirmation prompt for Bash scripts that accepts any case, has an optional default and handles cron, pipes and EOF without looping forever.