r/programming • u/f311a • Jul 28 '26
A shell colon does nothing. Use it anyway. | Filip Roséen
https://refp.se/articles/your-shell-and-the-magic-colon
301
Upvotes
r/programming • u/f311a • Jul 28 '26
305
u/guygizmo Jul 28 '26
Reading this once again makes me realize how daft of a scripting language bash is. I've been programming since I was eight years old and I still can't quite wrap my head around its weird syntax or goofy edge cases. I probably would eventually if I had to use it every day but I use it just infrequently enough that it never quite clicks. Especially since I can usually use any of a number of other sensible scripting languages instead.
98
u/royalme Jul 28 '26
I think this is only useful if you work exclusively in an environment of other bash experts. Even then, I don't really like seeing clever use of :
The example used is familiar enough for any general developer to get the gist of in a glance
while the other is opaque and language specific.
Dense and clever code is not always better code.
30
u/JaggedMetalOs Jul 28 '26
"Yeah look how short and concise my script is!"
(Opens the file a week later)
"What does this script even do??"
24
u/valarauca14 Jul 28 '26
This (can) change the scoping of the variable by creating an entirely new variable (or temporary variable shadow depending on the shell). While
: ${VAR:=default-value}will preserve scoping without creating a new variable.It really isn't preference. One has objectively fewer side effects, is easier to reason about, and has standardized behavior under POSIX.
7
u/marvin_sirius Jul 28 '26
"Everyone wants the colon" - Larry Wall
7
u/_kst_ Jul 29 '26 edited Jul 29 '26
In bash and other Bourne-like shells, the
:command is equivalent to thetruecommand. It takes zero or more arguments and does nothing (but the arguments might have side effects).I most commonly use it as a loop condition:
Remember than the condition in an
if,whileetc. statement is a command, treated as true if the command succeeds and false if it fails.[is a command, nearly equivalent totestSo when you write:the
[and]aren't part of the shell syntax.[is a command, and]is the last argument to that command. You can equivalently write:I often take advantage of this by assigning a literal string
trueorfalseto a variable, so I can use the variable (which expands to a command name) as a simple condition:39
u/cpitchford Jul 28 '26
: can be replaced
Made me wary of using it
Sometimes, I really want a null command that can't be replaced, like if, while, for can't be replaced
4
u/zeekar Jul 28 '26
Oh, this is just using the no-op command for the side-effects of parameter expansion. Sure, do it all the time:
Back before it borrowed the
#syntax from csh,:was the only way to do "comments" in the Bourne shell. But since it didn't prevent parameter expansion and so on you could still cause syntax errors with those alleged comments. Good times.2
u/darkslide3000 Jul 29 '26
He missed the more common one, which is to use it with math assignment operators (bash only):
2
u/wPatriot Jul 29 '26
The article tells me to use the colon. Gives me a description of what the colon does, but doesn't explain why it should be used. Then it blames the reader for that, because surely they would not have been able to comprehend an article explaining what the colon does and what a good use case for it is.
Boy do I feel inspired.
4
u/larsga Jul 28 '26
I have a better suggestion: don't use it. In fact, don't write anything in bash.
1
u/13steinj Jul 29 '26
This is a really buried lede. This trick is gold.
1
u/dada_ Jul 29 '26
I wrote all my personal shell automation in Fish Shell which is easier to write in than Bash for me, but I'm not even really a fan of Fish syntax either.
What I really want is the convenience of being able to run shell commands directly, with a conventional syntax (C-like or Python like, I don't care), basic primary types like strings/numbers/nestable arrays/dicts, first class functions and imports. I'm sure there's something like this by now so I'll have to do some digging.
Obviously I'll still be writing bash scripts when I need other people to be able to run them, though.
-46
u/IonTichy Jul 28 '26
nice emdashes
2
u/[deleted] Jul 28 '26
[removed] — view removed comment