Hasty Briefsbeta

Bilingual

A shell colon does nothing. Use it anyway.

8 hours ago
  • A single colon can replace multiple lines for checking required arguments using parameter expansion `${1:?error message}` which prints diagnostic and exits if variable is unset or empty.
  • The null command `:` evaluates its arguments but discards the result, enabling side effects like setting default values, truncating files, or checking file readability/writability.
  • Colon has historical roots in the 1971 Thompson shell as a label and comment marker.
  • Other uses of colon include: setting defaults (`: "${VAR:=value}"`), truncating files (`: > file`), checking file access (`(: < file) && echo YES`), and providing a command for `trap`.
  • The null command combined with parameter expansion reduces typing and potential typos compared to traditional assignment patterns.