bash - bash 中的 ":"被称为什么?冒号用作传递语句

标签 bash

参见:What is the Bash equivalent of Python's pass statement

这个“:”冒号用法叫什么?例如:

if [[ -n $STRING ]]; then
    #printf "[INFO]:STRING: if -n string: STRING:$STRING \n"
    : 
else
    printf "[INFO]:Nothing in the the string\n"
fi

最佳答案

具体而言,请在 shell 中运行 help :。它给出:

$ help :
:: :
    Null command.

    No effect; the command does nothing.

    Exit Status:
    Always succeeds.

在单行无限循环中非常有用,例如:

while :; do date; sleep 1; done

同样,您可以使用 true 而不是 : 编写相同的内容,但这更短。

有趣的是:

$ help true
true: true
    Return a successful result.

    Exit Status:
    Always succeeds.

根据这个,不同的是:是“Null command”, 而 true 是“返回成功的结果”。 另一个区别是 true 通常是真正的二进制文件:

$ which true
/usr/bin/true

另一方面,which : 没有给出任何结果。 (这是有道理的,作为一个“空命令”。)

无论如何,@Andy是的,这是 this other post 的重复项,这更好地解释了它。

关于bash - bash 中的 ":"被称为什么?冒号用作传递语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26897554/

相关文章:

bash - 有没有更好的方法在 bash 中运行 N 次命令?

linux - 如何使用shell变量更改特定位置的字符

bash 脚本无法按预期匹配文件

bash - 如何在 shell 脚本中转义连字符(-)?

linux - 搜索具有确切日期的多个文件,然后 tar 和 log

bash - bash 中英文单词的单数化功能

linux - bash/sh if 语句语法

linux - 如何在python中编写一个可以打开一个新的linux终端并自动执行源代码的脚本

linux - 合并两个 bash 历史文件

linux - 如何将 Linux bash 脚本输出定向到每台主机一行