Live data from Hacker News

A shell colon does nothing. Use it anyway

refp.se

11–20 of 191 posts

Re: A shell colon does nothing. Use it anyway

#11

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

First of all, I must salute you on the joke; well put haha!

---

It is a (contrived) example of usage where a command is required and `:` can fill in the blanks. There are certainly scenarios where negating an expression becomes harder than doing the "dumb" way, and I for one has written code where `:` can be used as a placeholder meaning "fill this in later" or equivalent.

With that said, 100% agree with you that in actual "production" code - there is always a cleaner way.

Re: A shell colon does nothing. Use it anyway

#12
post #5

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.

It turns out that LLMs are really good at writing bash too. even perl! maybe we should rethink some of these lost bits because we no longer need to worry about the arcane parts.

Define "really good?" I don't think I've ever had them produce a script that I didn't need to correct in some way. They can write it, that's true, but we still need to be able to read it.

Re: A shell colon does nothing. Use it anyway

#14

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.

Just use Fish

Re: A shell colon does nothing. Use it anyway

#15
post #5

Earlier quoted context omitted.

It turns out that LLMs are really good at writing bash too. even perl! maybe we should rethink some of these lost bits because we no longer need to worry about the arcane parts.

Define "really good?" I don't think I've ever had them produce a script that I didn't need to correct in some way. They can write it, that's true, but we still need to be able to read it.

Fair. I'm comparing it to the output I see generated in more mainstream, day to day, languages. I'm not going to bother to get into an argument on if the frontier models can generate amazing code in general.

What I've found is I can get get the frontier models to generate bash scripts, perl one liners, etc that do exactly what I need at roughly the same quality as any other code it generates.

Re: A shell colon does nothing. Use it anyway

#17
post #2

I am not a huge fan of most of these, but a few do seem useful. : "${1:?missing argument, aborting!}" I wouldn'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. Many of the others (like truncating files) are probably more clearly written with dedicated commands, but may come in use…

Agreed; author does note

> Why use the null-command when I could do VAR=${VAR:-default-value}?

and points out it's one less thing to typo, but that assumes the name is the same; I like e.g.

  TARGETFILE="${1:?need input file}"
  OPTIONALVAL="${2:-defaultvalue}"

Re: A shell colon does nothing. Use it anyway

#18
post #15

Earlier quoted context omitted.

Define "really good?" I don't think I've ever had them produce a script that I didn't need to correct in some way. They can write it, that's true, but we still need to be able to read it.

Fair. I'm comparing it to the output I see generated in more mainstream, day to day, languages. I'm not going to bother to get into an argument on if the frontier models can generate amazing code in general. What I've found is I can get get the frontier models to generate bash scripts, perl one liners, etc that do exactly what I need at roughly the same quality as any other code it generates.

100% They're super useful and I often use them to generate bash scripts of, but that's exactly how I know how important it is to check their work!

I'm a shell scripter at heart, the arcane stuff has always been a delight for me, so I've driven them to do some pretty complex stuff where previously I would have "copped out" and used python. I'd say the general adage of them being at the level of a very talent junior holds true.

Re: A shell colon does nothing. Use it anyway

#19

the truncation is a poor example. - it creates the file if it does not exist, not merely truncate. as a tutorial kind of blog post this incomplete description matters IMO. - it would work the same without the colon (similar for default variable assignment examples). we generally strive not to have "extra" things, like useless use of cat. - educationally it's useful to demonstrate that redirection, like parameter expa…

[deleted]

Re: A shell colon does nothing. Use it anyway

#20

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

Aside: hacker news doesn't do markdown code blocks, but instead uses 2 spaces before text;

   if x then :; else something; fi
Post reply on HN