A shell colon does nothing. Use it anyway

This article explains the utility of the colon command in Unix-like shell scripting, specifically focusing on its use in parameter expansion for error handling. It demonstrates how a single colon can replace multi-line if-statements to validate mandatory script arguments.
Why it matters
It provides practical, efficiency-focused coding techniques for developers working in command-line environments.
I've written more shell scripts than I can count, but I still stumble upon tricks that honestly blow my mind far too often than I care to admit. Latest thing that blew my head clean off? The shell colon.
... there was once a far too cold cup of coffee next to a freshly brewed far-too-hot one. Four different terminals where three could have been closed an hour ago, and a shell script which I really (really) did not want to write.
Who would have thought a single colon would be the one to save the day night?
Note : Want your mind blown straight away? See more colons in the limelight .
This is a familiar dance, it's pretty much muscle memory by this point. You have a script, it takes a few arguments, and some of them are mandatory; alright, an if-statement like so many times before:
Get smarter about the news
Sign up free for a feed built around what you actually care about, Dive Deeper research on any story, and the full text of every article.
Create free accountAlready have an account? Sign in