by VSpike · 2y ago · 0 comments
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.
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?
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...
for(;;){
// loop body here
}