:(冒号)GNU Bash 内置的目的是什么? [英] What is the purpose of the : (colon) GNU Bash builtin?

查看:36
本文介绍了:(冒号)GNU Bash 内置的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个什么都不做的命令的目的是什么,只不过是一个评论领导者,但实际上是一个内置的shell?

What is the purpose of a command that does nothing, being little more than a comment leader, but is actually a shell builtin in and of itself?

每次调用时,它比在脚本中插入注释慢 40% 左右,这可能会因注释的大小而有很大差异.我能看到的唯一可能原因是:

It's slower than inserting a comment into your scripts by about 40% per call, which probably varies greatly depending on the size of the comment. The only possible reasons I can see for it are these:

# poor man's delay function
for ((x=0;x<100000;++x)) ; do : ; done

# inserting comments into string of commands
command ; command ; : we need a comment in here for some reason ; command

# an alias for `true' (lazy programming)
while : ; do command ; done

我想我真正想要的是它可能具有的历史应用.

I guess what I'm really looking for is what historical application it might have had.

推荐答案

历史上,Bourne shell 没有 truefalse 作为内置命令.true 被简单地别名为 :,而 false 为类似 let 0 的别名.

Historically, Bourne shells didn't have true and false as built-in commands. true was instead simply aliased to :, and false to something like let 0.

:true 略好于可移植到古代 Bourne 派生的 shell.作为一个简单的例子,考虑既没有 ! 管道操作符也没有 || 列表操作符(一些古老的 Bourne shell 就是这种情况).这使得 if 语句的 else 子句成为基于退出状态进行分支的唯一方法:

: is slightly better than true for portability to ancient Bourne-derived shells. As a simple example, consider having neither the ! pipeline operator nor the || list operator (as was the case for some ancient Bourne shells). This leaves the else clause of the if statement as the only means for branching based on exit status:

if command; then :; else ...; fi

由于 if 需要一个非空的 then 子句并且注释不算作非空,: 用作非空子句操作.

Since if requires a non-empty then clause and comments don't count as non-empty, : serves as a no-op.

现在(即:在现代语境中)您通常可以使用 :true.两者都由 POSIX 指定,有些发现 true 更易于阅读.然而,有一个有趣的区别:: 是所谓的 POSIX 特殊内置,而 true常规内置在.

Nowadays (that is: in a modern context) you can usually use either : or true. Both are specified by POSIX, and some find true easier to read. However there is one interesting difference: : is a so-called POSIX special built-in, whereas true is a regular built-in.

  • 特殊的内置函数需要内置到shell中;常规内置插件只是通常"内置的,但并不严格保证.大多数系统的PATH中通常不应该有一个名为:的具有true功能的常规程序.

  • Special built-ins are required to be built into the shell; Regular built-ins are only "typically" built in, but it isn't strictly guaranteed. There usually shouldn't be a regular program named : with the function of true in PATH of most systems.

可能最关键的区别在于,对于特殊的内置函数,内置函数设置的任何变量 - 即使在简单命令评估期间的环境中 - 在命令完成后仍然存在,如此处使用 ksh93 所示:

Probably the most crucial difference is that with special built-ins, any variable set by the built-in - even in the environment during simple command evaluation - persists after the command completes, as demonstrated here using ksh93:

$ unset x; ( x=hi :; echo "$x" )
hi
$ ( x=hi true; echo "$x" )

$

请注意,Zsh 和 GNU Bash 一样忽略了这一要求,除非在 POSIX 兼容模式下运行,但所有其他主要的POSIX sh 派生"shell 都遵守这一点,包括 dash、ksh93 和 mksh.

Note that Zsh ignores this requirement, as does GNU Bash except when operating in POSIX compatibility mode, but all other major "POSIX sh derived" shells observe this including dash, ksh93, and mksh.

另一个区别是常规内置函数必须与 exec 兼容 - 此处使用 Bash 进行演示:

Another difference is that regular built-ins must be compatible with exec - demonstrated here using Bash:

$ ( exec : )
-bash: exec: :: not found
$ ( exec true )
$

  • POSIX 还明确指出 : 可能比 true 更快,尽管这当然是特定于实现的细节.

  • POSIX also explicitly notes that : may be faster than true, though this is of course an implementation-specific detail.

    这篇关于:(冒号)GNU Bash 内置的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆