:(冒号)GNU Bash内置的目的是什么?

本文翻译自:What is the purpose of the : (colon) GNU Bash builtin?

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? 什么都不做,仅是注释领导者,但实际上是内置于其自身的shell的目的是什么?

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. 这比每次调用将注释插入脚本要慢40%,这取决于注释的大小,差别可能很大。 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. 我想我真正要寻找的是它可能具有的历史应用。


#1楼

参考:https://stackoom.com/question/DWwA/冒号-GNU-Bash内置的目的是什么


#2楼

: can also be for block comment (similar to /* */ in C language). :也可以用于块注释(类似于C语言中的/ * * /)。 For example, if you want to skip a block of code in your script, you can do this: 例如,如果您想跳过脚本中的代码块,则可以执行以下操作:

: << 'SKIP'

your code block here

SKIP

#3楼

Two more uses not mentioned in other answers: 其他答案中未提及的另外两种用途:

Logging 记录中

Take this example script: 使用以下示例脚本:

set -x
: Logging message here
example_command

The first line, set -x , makes the shell print out the command before running it. 第一行set -x使外壳程序在运行之前打印出命令。 It's quite a useful construct. 这是一个非常有用的构造。 The downside is that the usual echo Log message type of statement now prints the message twice. 缺点是现在通常的echo Log message类型的语句将消息打印两次。 The colon method gets round that. 冒号方法可以解决这个问题。 Note that you'll still have to escape special characters just like you would for echo . 请注意,您仍然必须像echo一样转义特殊字符。

Cron job titles Cron职位

I've seen it being used in cron jobs, like this: 我已经看到它在cron作业中使用,像这样:

45 10 * * * : Backup for database ; /opt/backup.sh

This is a cron job that runs the script /opt/backup.sh every day at 10:45. 这是一项cron作业,每天10:45运行脚本/opt/backup.sh The advantage of this technique is that it makes for better looking email subjects when the /opt/backup.sh prints some output. 此技术的优点是,当/opt/backup.sh打印一些输出时,它可使电子邮件主题看起来更好。


#4楼

Historically , Bourne shells didn't have true and false as built-in commands. 从历史上看 ,Bourne shell没有像内置命令那样具有truefalse true was instead simply aliased to : , and false to something like let 0 . 相反,只将true别名为: ,将false别名为let 0

: is slightly better than true for portability to ancient Bourne-derived shells. :对于移植到古代Bourne衍生的炮弹而言,它比true要好得多。 As a simple example, consider having neither the ! 作为一个简单的示例,请考虑不使用! pipeline operator nor the || 管道运算符或|| list operator (as was the case for some ancient Bourne shells). 列表运算符(某些古老的Bourne贝壳就是这种情况)。 This leaves the else clause of the if statement as the only means for branching based on exit status: 这将if语句的else子句作为基于退出状态进行分支的唯一方法:

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. 由于if需要一个非空的then子句和注释,则不算作非空的,因此:用作no-op。

Nowadays (that is: in a modern context) you can usually use either : or true . 如今 (即:在现代环境中),您通常可以使用:true Both are specified by POSIX, and some find true easier to read. 两者都是由POSIX指定的,有些true容易阅读。 However there is one interesting difference: : is a so-called POSIX special built-in , whereas true is a regular built-in . 然而,有一个有趣的差异: : 内置了一个所谓的POSIX 特殊的 ,而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. 通常有不应该命名为正常计划:与功能true在大多数系统的路径。

  • 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: 可能最关键的区别在于,使用特殊的内置程序,即使在简单的命令评估过程中,内置程序设置的任何变量(即使在环境中)也将在命令完成后仍然存在,如以下使用ksh93所示:

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

    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. 请注意,除了在POSIX兼容模式下运行时,Zsh都会忽略此要求,GNU Bash也会忽略此要求,但是所有其他主要的“ POSIX sh派生”外壳程序都遵守此要求,包括破折号,ksh93和mksh。

  • Another difference is that regular built-ins must be compatible with exec - demonstrated here using Bash: 另一个区别是常规的内置程序必须与exec兼容-在此处使用Bash进行了演示:

     $ ( exec : ) -bash: exec: :: not found $ ( exec true ) $ 
  • POSIX also explicitly notes that : may be faster than true , though this is of course an implementation-specific detail. POSIX还明确指出:可能比true快,尽管这当然是特定于实现的细节。


#5楼

You could use it in conjunction with backticks ( `` ) to execute a command without displaying its output, like this: 您可以将其与反引号( `` )结合使用以执行命令而不显示其输出,如下所示:

: `some_command`

Of course you could just do some_command > /dev/null , but the : -version is somewhat shorter. 当然,您可以执行some_command > /dev/null ,但是: -version会更短一些。

That being said I wouldn't recommend actually doing that as it would just confuse people. 话虽这么说,但我不建议您实际这样做,因为这会让人们感到困惑。 It just came to mind as a possible use-case. 只是想到了一个可能的用例。


#6楼

It's similar to pass in Python. 类似于在Python中pass

One use would be to stub out a function until it gets written: 一种用途是将一个函数存根,直到函数被写入:

future_function () { :; }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章