"while :" vs. "while true"

Bash

Bash Problem Overview


When you look at how infinite loops should be implemented, you mostly see this approach:

while :
do
  # loop infinitely
done

But I just don't understand the use of : here. Wouldn't it be better to use:

while true
do
  # loop infinitely
done

?

Bash Solutions


Solution 1 - Bash

from manual:

> : [arguments] > No effect; the command does nothing beyond expanding arguments and performing any specified > redirections. A zero exit code is returned.

As this returns always zero therefore is is similar to be used as true

Check out this answer: What Is the Purpose of the `:' (colon) GNU Bash Builtin?

Solution 2 - Bash

The colon is a built-in command that does nothing, but returns 0 (success). Thus, it's shorter (and faster) than calling an actual command to do the same thing.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhelpermethodView Question on Stackoverflow
Solution 1 - BashphoxisView Answer on Stackoverflow
Solution 2 - BashunwindView Answer on Stackoverflow