After a while, you probably know more commands and utilities than you know what to do with, and you'll forget they exist when you need them. In order to not waste time looking for a program for a particular and infrequent purpose, I create "do nothing" aliases like `: alias f3probe` so I can realize I just forgot I already have something for it. Nice predictable pattern to grep with `^: alias` to look through all of these.
A shell colon does nothing. Use it anyway
91–100 of 191 posts
Re: A shell colon does nothing. Use it anyway
#92Why take a perfectly readable if-statement and turn it into something, 99.9% of people would need to lookup. Concise != better. You can make it one line with: [ -z "$1" ] && { echo "missing argument, aborting." 1>&2; exit 1 }
I believe you missed a semicolon after `exit 1` and before `}`. `}` closes the opening `{` only at the start of a command (POSIX rules; don't remember now if bash recognizes it when it's just a command argument).
[ -z "$1" ] && echo "fail" >&2 && exit 1Re: A shell colon does nothing. Use it anyway
#93Nice one! I love those weird bash tricks. Some of the examples here are interesting, but they show parameter substitution more than colon itself: https://tldp.org/LDP/abs/html/parameter-substitution.html In small scopes, I tend to inline the `:?` validation inside the arg of the command. `echo "${1:? first param required}"` Another usecase is to use colon in the body of a while loop, while doing work in the condition…
The extent to which the script author gets to feel smart and efficient is exactly the extent to which the future reader of the script gets to feel like an idiot.
Re: A shell colon does nothing. Use it anyway
#94For example, what does `$foo` mean in shell syntax? In any reasonable language (perl or powershell, for example, or python if you drop the `$`), it's an expression that evaluates to whatever value's inside that variable. In shell, `$foo` isn't an expression in that sense, and what it does depends on what's inside it via a variety of string substitution rules.
This is the main reason we have arcane articles like this.
That said, nice article.
Re: A shell colon does nothing. Use it anyway
#95Nice one! I love those weird bash tricks. Some of the examples here are interesting, but they show parameter substitution more than colon itself: https://tldp.org/LDP/abs/html/parameter-substitution.html In small scopes, I tend to inline the `:?` validation inside the arg of the command. `echo "${1:? first param required}"` Another usecase is to use colon in the body of a while loop, while doing work in the condition…
I hate weird bash tricks with a passion. The extent to which the script author gets to feel smart and efficient is exactly the extent to which the future reader of the script gets to feel like an idiot.
Re: A shell colon does nothing. Use it anyway
#96life 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.
(...)
> especially in the age of LLMs
Funny way of putting it.
But I think you are right still :)
Re: A shell colon does nothing. Use it anyway
#97Articles like this are fun but they all come from posix shell syntax being fundamentally bad for scripting/programming. All the piping stuff is great, of course. And the overall ecosystem is great. But the interpretation of the script itself working by a series of string substitutions is a mechanism we wouldn't accept in a regular programming language. And there's no excuse for it really, except that shell syntax is…
Re: A shell colon does nothing. Use it anyway
#98Articles like this are fun but they all come from posix shell syntax being fundamentally bad for scripting/programming. All the piping stuff is great, of course. And the overall ecosystem is great. But the interpretation of the script itself working by a series of string substitutions is a mechanism we wouldn't accept in a regular programming language. And there's no excuse for it really, except that shell syntax is…
Bash et al are great for command line use, but produce a situation of really bad engineering hygiene when used in scripts.
Do not use. Stay away. Bash et al considered harmful. Red flags on job interview when referenced.
Re: A shell colon does nothing. Use it anyway
#99Switched from bash to plain python scripts for shell stuff everywhere several years ago, and never looked back into bash zoo anymore. Stable syntax across Win/Mac/Linux, no bash/zsh/msys2 obscure differences, normal errors and Clause writes scaffolds quick and flawless anyway
Re: A shell colon does nothing. Use it anyway
#100Earlier quoted context omitted.
I believe you missed a semicolon after `exit 1` and before `}`. `}` closes the opening `{` only at the start of a command (POSIX rules; don't remember now if bash recognizes it when it's just a command argument).
In this specific case, echo can't fail to there's no need for any of that: [ -z "$1" ] && echo "fail" >&2 && exit 1