A shell colon does nothing. Use it anyway
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? Published23 July 2026 at UTCModified26 July 2026 at UTCAuthorFilip RoséenTags#shell #posix #unix Table of Contents In a land far-far away.. Checking for required argumentsParameter expansion and the story of :? That.. other colon More colons in the limelight Con-colon-sion Frequently Asked QuestionsWhy do I need the null-command? Doesn't the expansion happen without the colon? Why use the null-command when I could do VAR=${VAR:-default-value}? In a land far-far away.. # ... 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? Checking for required arguments # This is a familiar dance, it's pretty much muscle memory . You have a script, it takes a few arguments, and some of them are mandatory; alright, an if-statement like so many times before: if [ -z "$1" ]; then echo "missing argument, aborting." 1>&2 exit 1 fi echo "Hello $1!" Though.. what if I told you the above four lines could be replaced by just... one? : "${1:? missing argument, aborting.}" echo "Hello $1!" $ bash example. sh example. sh: line 1: 1: missing argument, aborting. $ bash example. sh refp Hello refp! And look what happens if we refer to a variable with a proper name — it's the same behavior as previously but easier to spot; the diagnostic includes the name of our variable! : "${GREET_NAME:? missing argument, aborting.}" echo "Hello $GREET_NAME!" $ bash greet. sh greet. sh: line 1: GREET_NAME: missing argument, aborting. Parameter expansion and the story of :? # There are two things going on in the previous snippet, and you are correct in identifying that one part is using parameter expansion: The syntax ${name:? diagnostic} checks whether $name is unset or empty — if it is, the diagnostic is printed to stderr and the shell exits with a non-zero status, otherwise; if the variable is set, it is equivalent to $name. That.. other colon # So that's one colon, but what about that other one, the one who sits alone at the beginning of the line? : is the null-command — a builtin that does nothing but evaluate its arguments and discard the result. : is old — it goes all the way back to the 1971 Thompson shell where it doubled as a label and Unix's very first comment marker. : two eyes staring at you in the dark, with love.
Original story by Hacker News • View original source
Anonymous Discussion
Real voices. Real opinions. No censorship. Resets in 2 hours.
About NewsBin
Freedom of speech first. Anonymous discussion on today's news. All content resets every 24 hours.
No accounts. No tracking. No censorship. Just honest conversation.
Loading comments...