bash "while :" vs. "while true"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10797835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:09:57  来源:igfitidea点击:

"while :" vs. "while true"

bash

提问by helpermethod

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

?

?

采纳答案by phoxis

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

由于这始终返回零,因此类似于用作 true

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

看看这个答案:':'(冒号)GNU Bash 内置函数的目的是什么?

回答by unwind

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

冒号是一个内置命令,什么都不做,但返回 0(成功)。因此,它比调用实际命令来做同样的事情更短(也更快)。