Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I definitely think if you want to use `cat` then just go ahead, it's fine. Sometimes these things are a power play, a way to distinguish between people who know the social codes and those who don't. In this case, it probably had a reasonable origin even if it's now more of a way to beat on newcomers. On old systems, memory was limited, disk was slow and forking was expensive. Saving a process in a script or one liner was a noticeable improvement performance-wise.

I learned some bash from an old-timer who would write an infinite-loop like this:

  while :; do 
    # loop body here
  done
This works because the `:` is a way to set a label, and it implicitly returns 0. It's just a weird wrinkle of the language. So, why not do `while true`? On old systems, `true` was not a builtin and would call `/usr/bin/true`. Writing the loop this way saves a process fork on each iteration.

On a modern system, you'd be hard pushed to measure the difference, so it really doesn't matter which style you prefer.



> This works because the `:` is a way to set a label, and it implicitly returns 0. It's just a weird wrinkle of the language.

Do you have a source for that? I thought it was just POSIX built-in for true. Like `.` vs. `source`. What's a label in this context anyway?


Hah, yeah I was completely wrong on that! Should have fact checked myself. That's a falsehood I absorbed at some point and didn't question.


Well there's a blog post idea for you, sounds primed for the front page already - Falsehoods Programmers Believe About Colons...


: is the prefix for labels in DOS batch files.


No need to ask for a source. The word "label" in the POSIX shell documentation only occurs in the description of `case`, and it doesn't happen in the manual page for bash, dash, zsh, etc.


Equally surprised. I know ':' is a label in sed, but labels in (ba)sh, I'm not aware. If it's indeed a label, is there a goto?


> This works because the `:` is a way to set a label, and it implicitly returns 0.

Nope. Unix shell doesn't have labels (are you mixing with DOS batch files?).

: is a shell builtin that does nothing. In the bash man page, look for the first entry of the "SHELL BUILTIN COMMANDS" section. https://www.gnu.org/software/bash/manual/html_node/Bourne-Sh...


infinite loop in C:

  for(;;){
      // loop body here
  }




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: