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

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

问题描述

什么是,什么也不做,比评论的领导者多一点命令的目的,但实际上是一个壳本身并内建?

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.

推荐答案

历史后,伯恩炮弹没有真正内置的命令。 真正已不是简单别名来像 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.

真正稍好移植到古老的Bourne衍生弹。举一个简单的例子,考虑既没有在管道运营商,也不是 || 列表操作符(因为是为案件一些古老的贝壳伯恩)。这使得如果语句作为唯一手段的其他条款的基础上退出状态分支:

: 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

由于如果需要一个非空然后子句和评论不计入非空,作为一个空操作

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

目前(即:在现代背景下),你通常可以使用任何真正。两者都是由POSIX规定,有的找到真正更容易阅读。但是有一个有趣的差异:是一个所谓的POSIX的特殊的内置的,而真正常规内置

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.


  • 必须采取特别的内置插件被内置到外壳;定期内置插件是唯一的典型的内置的,但它是不严格保证。有通常不应该命名为正常计划与功能真正在大多数系统中的路径

  • 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炮弹遵守本,包括仪表板,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也明确指出,可能会快于真正,虽然这当然是一个的实现特定的细节。

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

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

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