Hacker News Viewer

A shell colon does nothing. Use it anyway

by olexsmir on 7/25/2026, 1:33:49 PM

https://refp.se/articles/your-shell-and-the-magic-colon

Comments

by: zaptheimpaler

life is way too short to deal with this nightmare of a language and its 50000 footguns for anything longer than a 2 line script, especially in the age of LLMs. Just write a python/TS/any real language script instead. Bash is great for the command line, it should be limited to use there.

7/26/2026, 5:28:35 AM


by: kevincox

I am not a huge fan of most of these, but a few do seem useful.<p><pre><code> : &quot;${1:?missing argument, aborting!}&quot; </code></pre> I wouldn&#x27;t use this because I would want to give $1 a name for the rest of the script, so I would assign. But it can be a nice way to give a clear error for missing required environment variables.<p>Many of the others (like truncating files) are probably more clearly written with dedicated commands, but may come in useful if you are going to extreme lengths to avoid dependencies outside of the shell.

7/25/2026, 1:52:03 PM


by: normie3000

Prefer<p>``` if x then :; else something; fi ```<p>over<p>``` if ! x; then something; fi ```<p>Really? Colon is the appendix of the shell.

7/26/2026, 5:55:16 AM


by: jiveturkey

the truncation is a poor example.<p>- it creates the file if it does not exist, not merely truncate. as a tutorial kind of blog post this incomplete description matters IMO.<p>- it would work the same without the colon (similar for default variable assignment examples). we generally strive not to have &quot;extra&quot; things, like useless use of cat.<p>- educationally it&#x27;s useful to demonstrate that redirection, like parameter expansion, works before the command executes (the null command in this case), but the article doesn&#x27;t explain that at all!<p>otherwise i &lt;3 this article. some uses of colon i had never thought of or seen before. like file truncation, not sure i&#x27;d use them but it was cool to see them.

7/25/2026, 8:02:55 PM