A shell colon does nothing. Use it anyway

293 points · 122 comments on HN · read original →

Points and comments are a snapshot, not live.

The shell null command (`:`) does nothing but has practical uses in parameter expansion and argument validation.

The shell colon (`:`) is a null command dating back to the 1971 Thompson shell. It evaluates arguments and discards results. Practical uses include: checking required arguments with `${1:?error message}`, setting default values with `${VAR:=default}`, truncating files via redirection (`: > error.log`), testing file readability/writability, fulfilling the trap command requirement, checking variables under `set -u`, and filling empty branches. The author argues it reduces typos versus traditional variable assignment, citing `: "${DATA_DIR:=/var/data}"` as a single-mention alternative to `DATA_DIR="${DATA_DRI:-/var/data}"`.

What commenters are saying

Commenters were split. Some found the `${1:?error}` pattern useful for required environment variables but preferred explicit usage/help-text for named arguments. Others criticized examples like file truncation as incomplete (creates files, not just truncates) and noted redirection works without the colon. A camp argued against shell scripting complexity, advocating Python/TypeScript instead. Correction: the if-statement example showing `if x then :; else something; fi` was criticized as inferior to `if ! x; then something; fi`.