Live data from Hacker News

A shell colon does nothing. Use it anyway

refp.se

91–100 of 191 posts

Re: A shell colon does nothing. Use it anyway

#91
I have probably the worst use case, but I like it. I have a very specifically structured ZDOTDIR, and I write everything in a way that is self-documenting.

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.

Re: A shell colon does nothing. Use it anyway

#92
post #79

Why 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).

In this specific case, echo can't fail to there's no need for any of that:

  [ -z "$1" ] && echo "fail" >&2 && exit 1

Re: A shell colon does nothing. Use it anyway

#93
post #74

Nice 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

#94
Articles 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 really, really old.

For 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

#95
post #93
post #74

Nice 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.

The only upside of weird bash tricks is there's so much bash out there that the LLMs are really well trained on it - so you won't have to read the script, just the tests.

Re: A shell colon does nothing. Use it anyway

#96

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.

> 50000 footguns

(...)

> 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

#97

Articles 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…

Your `$foo` example confused me at first because I thought the backticks are part of the example.

Re: A shell colon does nothing. Use it anyway

#98

Articles 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…

Yes a large percentage of bash scripts fails if you feed them filenames with spaces or other special characters. Or a large amount of filenames near the Unix maximum commandline length.

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

#99
This "hidden knowledge" is fun to read but pain to remember and use, especially when working with multi-platform environments //

Switched 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

#100
post #92
post #79

Earlier 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

Echo will fail here when stderr is closed. For example when the caller of the script pipes stderr to another command and that command exits.
Post reply on HN