~/ronggeng: a developer's notebook since 2009

Practical programming notes, tested and explained.

PHP, Python and JavaScript, Linux and the command line, databases and the web, plus a long-running library of pure Bash functions named after their PHP counterparts. Every example is run before it is published.

Latest articles

All 51 articles

Categories

Programming

PHP, Python, JavaScript and Git: language features and habits worth knowing.

All 7

Linux and the command line

Processes, services, networking and the tools you reach for every day.

All 7

Web

HTTP, caching, redirects and running sites without surprises.

All 6

MySQL

Stored functions, data types and queries for real data.

All 3

ipcalc()

Network, Broadcast and Prefix From a Netmask

Work out the network, broadcast address and prefix length of an IPv4 address inside a query, using INET_ATON(), INET_NTOA() and a little bitwise...

Series

The pure Bash function library

Started in 2009: small shell functions named after their PHP counterparts (__pgrepf(), _execq(), basename(), dirname(), emptydir(), explode() and implode() and more), each rewritten, tested and explained.

Cheat sheet

Bash library: strings

Length, case, search, replace, pad and trim, the way PHP names them.

explode() and implode()

Split and Join Strings

PHP's explode() and implode() in Bash: read -ra and mapfile -d for splitting, "${arr[*]}" for joining, and functions that cope with multi-character...

lcfirst()

Lowercase the First Character

lcfirst() in Bash with ${var,} and a Bash 3.2 fallback, used for PascalCase to camelCase, plus a fix for names that begin with an acronym.

rm_str()

Remove a Substring From a String

Delete every occurrence of a substring with one Bash parameter expansion, no sed or tr needed, and the quoting mistakes that make it go wrong.

str_pad()

Pad a String to a Fixed Length

PHP-compatible str_pad() in pure Bash, when printf "%-10s" and %05d are enough, and why multibyte text breaks the alignment of your columns.

str_repeat()

Repeat a String N Times

A str_repeat() for Bash that stays fast at millions of copies, why the common printf trick slows down, and the two bugs in the 2009 version.

str_replace()

Replace Text in a String

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 &.

stristr()

Case-Insensitive Substring Search

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...

strlen()

Get the Length of a String

Bash's ${#var} counts characters, PHP's strlen() counts bytes. A clean strlen() function, a strbytes() companion, and the locale traps in between.

strstr()

Check if a String Contains a Substring

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.

strtolower()

Convert a String to Lowercase

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.

strtoupper()

Convert a String to Uppercase

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.

trim()

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.

ucfirst()

Uppercase the First Character

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".

ucwords()

Capitalise Every Word in a String

A ucwords() for Bash that works word by word instead of char by char, preserves all whitespace, and lets you decide what counts as a word boundary.

Bash library: arrays and input

Split, join, look up and validate what your script is given.

in_array()

Check if an Array Contains a Value

A correct in_array() for Bash, an associative-array set for many lookups, and a demonstration of why [[ "${arr[*]}" =~ value ]] gives false positives.

is_num()

Check if a Value Is a Number

Check that input is a whole number, a signed integer or a decimal before doing arithmetic, and avoid the octal, empty-string and injection problems...

yesno()

Ask a Yes/No Question Safely

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.

Bash library: paths, files and processes

File names, directories and running programs without forking.

__pgrepf()

pgrep -f Using Only /proc

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.

_execq()

Run a Command Quietly and Keep Its Status

Run a command silently and test only its exit status. Why "$@" beats storing a command in a string, and what to use for pipelines.

basename()

Strip the Directory From a Path

Get the file name from a path with parameter expansion, matching coreutils basename on trailing slashes, "/" and suffixes, without starting a process.

dirname()

Get the Directory Part of a Path

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.

emptydir()

Check Whether a Directory Is Empty

Test whether a directory is empty in pure Bash with nullglob and dotglob, handle hidden and unreadable directories, and know when find -quit is...

Writing safer Bash scripts

Strict mode, option parsing, cleanup and error handling.